如何获得从JSON解析return语句的结果? [英] How to get the result for return statement from JSON parsing?

查看:220
本文介绍了如何获得从JSON解析return语句的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照code从这里 使用JSON解析的价值,但我得到的问题在我的return语句。我想提出的解析结果到我return语句。怎么做?



这里是我的code:

 公共字符串目录注入(用户字符串,字符串密码)
    {
        SoapObject要求=新SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);        PI的PropertyInfo =新的PropertyInfo();
        pi.setName(ccduser);
        pi.setValue(用户);
        pi.setType(为String.class);
        request.addProperty(PI);        PI2的PropertyInfo =新的PropertyInfo();
        pi2.setName(密码);
        pi2.setValue(密码);
        pi2.setType(为String.class);
        request.addProperty(PI2);
        SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = TRUE;        envelope.setOutputSoapObject(请求);        HttpTransportSE httpTransport =新HttpTransportSE(SOAP_ADDRESS);
        尝试
        {
            httpTransport.call(SOAP_ACTION,信封);
            SoapObject resultSOAP =(SoapObject)envelope.bodyIn;            / *得到我们的结果JSON字符串* /
            串ResultObject = resultSOAP.getProperty(0)的ToString();            resultSOAP =(SoapObject)envelope.bodyIn;
            ResultObject = resultSOAP.getProperty(0)的ToString();            如果(ResultObject.startsWith({)){//如果JSON字符串是一个对象
                JSONObj =新的JSONObject(ResultObject);
                迭代器<串GT; ITR = JSONObj.keys();
                而(itr.hasNext()){
                    字符串键=(字符串)itr.next();
                    字符串值= JSONObj.getString(关键);
                    BundleResult.putString(键,值);
                    //的System.out.println(bundleResult.getString(关键));
                }
            }            否则,如果(ResultObject.startsWith([)){//如果JSON字符串数组
                JSONArr =新JSONArray(ResultObject);
                的System.out.println(长度+ JSONArr.length());
                的for(int i = 0; I< JSONArr.length();我++){
                    JSONObj =(JSONObject的)JSONArr.get(I)
                    BundleResult.putString(将String.valueOf(ⅰ),JSONObj.toString());
                    //的System.out.println(bundleResult.getString(I));
                }
            }
        }
        赶上(例外的例外)
        {        }        返回null;    }


如果您有JSON数组对象:

 的JSONObject jObject =新的JSONObject(*** JSON字符串你有***);
JSONArray contestantObjects = jObject.getJSONArray(***阵列名称***);
的for(int i = 0; I< contestantObjects.length();我++){
        mChannels.setId(contestantObjects.getJSONObject㈠.getString(ID)的toString());
        mChannels.setName(contestantObjects.getJSONObject㈠.getString(名称)的toString());
}

如果你只有一个项目:

 的JSONObject jObject =新的JSONObject(*** JSON字符串你有***);
米preview.setBody(jObject.getString(***项目名称***)的toString());
米preview.setPublishedDate(jObject.getString(publishedDate)的toString());
米preview.setRefKey(jObject.getString(refKey)的toString());
米preview.setTitle(jObject.getString(题)的toString());

http://jsonviewer.stack.hu/ 是解析JSON在线工具。它的真棒,你可以使用它。

I've follow the code for parsing the value with JSON from here, but I get the problem in my return statement. I want to put the parsing result into my return statement. How to do that?


Here is my code:

public String MASUK(String user, String password)
    {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

        PropertyInfo pi = new PropertyInfo();
        pi.setName("ccduser");
        pi.setValue(user);
        pi.setType(String.class);
        request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("password");
        pi2.setValue(password);
        pi2.setType(String.class);
        request.addProperty(pi2);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        try
        {
            httpTransport.call(SOAP_ACTION, envelope);
            SoapObject resultSOAP = (SoapObject) envelope.bodyIn;

            /* gets our result in JSON String */
            String ResultObject = resultSOAP.getProperty(0).toString();

            resultSOAP = (SoapObject) envelope.bodyIn;
            ResultObject = resultSOAP.getProperty(0).toString();

            if (ResultObject.startsWith("{")) { // if JSON string is an object
                JSONObj = new JSONObject(ResultObject);
                Iterator<String> itr = JSONObj.keys();
                while (itr.hasNext()) {
                    String Key = (String) itr.next();
                    String Value = JSONObj.getString(Key);
                    BundleResult.putString(Key, Value);
                    // System.out.println(bundleResult.getString(Key));
                }
            } 

            else if (ResultObject.startsWith("[")) { // if JSON string is an array
                JSONArr = new JSONArray(ResultObject);
                System.out.println("length" + JSONArr.length());
                for (int i = 0; i < JSONArr.length(); i++) {
                    JSONObj = (JSONObject) JSONArr.get(i);
                    BundleResult.putString(String.valueOf(i), JSONObj.toString());
                    // System.out.println(bundleResult.getString(i));
                } 
            }
        }
        catch (Exception exception)
        {

        }

        return null;

    }

解决方案

If you have JSON array object:

JSONObject jObject = new JSONObject(***JSON String you have ***);
JSONArray contestantObjects = jObject.getJSONArray("*** Array Name ***");
for(int i=0; i<contestantObjects.length(); i++) {
        mChannels.setId(contestantObjects.getJSONObject(i).getString("id").toString());
        mChannels.setName(contestantObjects.getJSONObject(i).getString("name").toString());
}

If you have just one item:

JSONObject jObject = new JSONObject(***JSON String you have ***);
mPreview.setBody(jObject.getString("*** Item Name ***").toString());
mPreview.setPublishedDate(jObject.getString("publishedDate").toString());
mPreview.setRefKey(jObject.getString("refKey").toString());
mPreview.setTitle(jObject.getString("title").toString());

http://jsonviewer.stack.hu/ is online tool for parsing JSON. its awesome you can use it.

这篇关于如何获得从JSON解析return语句的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆