0

我已经按照从此处使用 JSON 解析值的代码,但在我的 return 语句中遇到了问题。我想将解析结果放入我的 return 语句中。怎么做?


这是我的代码:

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;

    }
4

3 回答 3

1

如果您有 JSON 数组对象:

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());
}

如果您只有一项:

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/是解析 JSON 的在线工具。它很棒,你可以使用它。

于 2012-06-19T02:06:02.560 回答
0

参考您用于解析 JSON 字符串的链接,我建议您bundleResult从函数中返回对象。将您的方法的返回值更改为public Bundle MASUK(String user, String password).

编辑

不要忘记初始化 Bundle 对象:

private Bundle bundleResult=new Bundle();

并注意不要更改正在使用的变量的大小写(大写/小写)。我可以在您的代码中看到,BundleResult.putString(Key, Value);并且参考链接使用bundleResult.putString(Key, Value);

于 2012-06-19T03:04:32.540 回答
0
JSONObject prefsJson = new JSONObject(prefsJsonString);
String str = prefsJson.has("str") == true ? prefsJson.getString("str") : "";

那样行吗?

于 2012-06-19T01:52:48.790 回答