开发者

Json parsing problem android

If the response of a request is a json response how to handle it and decode it.I have tried the following abnd get an error @ JSONArray json = new JSONArray(r1);

HttpPost post = new HttpPost(postURL);
MultipartEntity reqEntity = new MultipartEntity();

HttpResponse response = client.execute(post);  
HttpEntity resEntity = response.getEntity();

String r1 = EntityUtils.toString(resEntity);
System.out.println("printing response now "+r1);
JSONArray json = new JSONArray(r1);


//Toast.makeText(getApplicationContext(), "data received"+r1, Toast.LENGTH_LONG).show();
//  JSONObject json = new JSONObject(r1);
JSONArray venues = json.getJSONObject("data")
          .getJSONArray("url")
          .getJSONObject(0)
          .getJSONArray("url"开发者_如何学编程);

Json structure is given below

 [
 {"data": 
  {"url": 
   {
     "url": "http://www.xxxxxx.com/story.html", "title":"some data","source_url": "www.somesite.com", "summary": "\n \n \n \n \n somedata again"
   }
  }
 }
 ]

Error:

   08-18 16:30:22.907: INFO/System.out(1178): Exceptionorg.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray


I've this following code taking your json and it works for me... May be you've to check if your orginal json string is ok ... byte per byte may be .. invisible character may disturb the parsing

  String r1 = "[{\"data\": {\"url\": { \"url\": \"http://www.xxxxxx.com/story.html\",  \"title\":\"some data\",\"source_url\": \"www.somesite.com\", \"summary\": \"\\n \\n \\n \\n \\n somedata again\"}}}]";
try {
   JSONArray json = new JSONArray(r1);

   Object url = json.getJSONObject(0)
            .getJSONObject("data")
            .getJSONObject("url")
            .get("url");
   Toast.makeText(getApplicationContext(), "url="+url.toString(), Toast.LENGTH_LONG).show();
   Log.i("TESTJSON","All Is Ok");

} catch (Exception e) {
   Log.d("TESTJSON","Something wrong..",e);
   Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜