0

JSON RPCのリンクを使用しました。期待通りの反応が得られています。しかし、応答を解析しようとすると、json エラーが発生します。

私のコード:

JSONEntity entity = new JSONEntity(jsonRequest);
    HttpPost request = new HttpPost("http://192.168.1.150/jsondemo12/service.asmx");
    request.setEntity(entity);
    HttpResponse response = httpClient.execute(request);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity httpEntity = response.getEntity();
        InputStream content = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content,"iso-8859-1"),8);
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        content.close();
    } else {
        Log.e(AndroidJSONActivity.class.toString(), "Failed to download file");
    }


    strJSONValue=builder.toString();

    txtViewParsedValue.append("\n+++++++++++++\n"+strJSONValue+"\n");
    try {
        parseJSON();
    } catch (JSONException e) {
        Log.e("error","Error while parsing!!!");
        e.printStackTrace();
    }
    Log.e("response", strJSONValue);
public void parseJSON() throws JSONException
    {
        String attr1="",attr2="";
        jsonObject = new JSONObject(strJSONValue);
        JSONArray  result = jsonObject.getJSONArray("result");    <- Error in this line!!!
        for(int i=0;i < result.length();i++){
            JSONObject e = result.getJSONObject(i);
            attr1 = "ExhibitorID: "+ e.getString("ExhibitorID");
            attr2 = "ExhibitorName: "+e.getString("ExhibitorName");
        }
        strParsedValue=attr1+"\n"+attr2;
        Log.d("Parse", attr1);
        Log.d("Parse", attr2);

        txtViewParsedValue.append("\n**********\nParsed Value: \n");
        txtViewParsedValue.append(strParsedValue);
    }

「strJSONValue」で取得した結果は、開始と終了の二重引用符のない文字列形式です。お気に入り:

{"id":2,"result":"[
{\"ExhibitorID\":42, etc....}
]"}

結果の文字列は要件どおりですが、要件に従って文字列を JSON オブジェクトに解析できません。Logcatでエラーが発生します:org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray

私を助けてください。ありがとう

4

1 に答える 1

1

結果は実際にはjson配列ではなく文字列を返しています。json形式が次のようになる場合、json配列が返されます

{
  "id": 2,
  "result": [
    {
      "ExhibitorID": 42
    }
  ]
}

現在、次の形式になっています。

{
  "id": 2,
  "result": "[ {\"ExhibitorID\":42, etc....} ]"
}
于 2012-01-18T08:17:36.690 に答える