2012-01-18 23 views
0

JSON RPCにlinkを使用しました。私は期待どおりの反応を得ています。しかし、私は応答を解析しようとすると、それは私にjsonエラーを与えている。JSONエラー「java.lang.String型の結果の値はJSONArrayに変換できません」android

マイコード:

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

i「が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

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

答えて

1

あなたの結果は実際にはjson配列ではなく文字列を返しています。あなたのJSON形式は、この

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

ようになるかどうかは、現在、それはこの形式であるJSON配列を返します:私は私のid.thereにあなたの履歴書を送る

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

でアンドロイドdevに関する開口部でありますinterwall –

+0

そう、私は受け取った文字列を変更する必要があり、その後、解析のためにそれを渡す? – Pallavi

+0

私は、json配列を作成しているときにwebserviceで文字列に変換していると思います。それを変換しないでください。受け取ったレスポンスを変更するのは良い習慣ではありません。まあ、解決策としては、getStringを使って文字列の "result tag"を取得し、新しいJSONObject(string)を使ってjsonオブジェクトの文字列を隠すことができます。その後、それを解析する。 –

関連する問題