当前位置: 动力学知识库 > 问答 > 编程问答 >

java - JSON ARRAY decoding in android

问题描述:

I have got this form server(php) end and unable to decode this using android(java). Can you please help me anyway?

{

"status": "success",

"message": [

{

"name": "abc abc abc",

"new_book_id": "49",

"h_p_y": "8 iso, new 345, 1234",

"comment": "1",

"rating": 5,

"image_link": "http://www.yahoo.com/cou/images/view.jpg",

"comment_array": [

{

"com": "good like",

"name": "android"

}

]

},

{

"name": "cdb cdbcdv",

"new_book_id": "1234",

"h_p_y": "345 wert",

"comment": "1",

"rating": 3,

"image_link": "http://www.yahoo.com/cou/images/view.jpg",

"comment_array": [

{

"com": "I took my wife for her first ever swim. great value for the price ",

"name": "Guest User"

},

{

"com": "",

"name": "Guest User"

}

]

}

],

"totalsize": 10

}

I can't able to get elements (com, name) from comment array.

I am trying this to decode in android end:

row = new JSONObject(jsonResult);

data = row.getJSONArray("message");

for (int i = 0; i < data.length(); i++) {

HashMap<String, String> storeValues = new HashMap<String, String>();

row = data.getJSONObject(i);

data1 = row.getJSONArray("comment_array");

for(int k=0;k<data1.length();k++){

Toast.makeText(getApplicationContext(), "hellow"+k, Toast.LENGTH_SHORT).show();

}

storeValues.put(NAME, row.getString("name"));

storeValues.put(ABC,row.getString("h_p_y"));

storeValues.put(IMAGE_URL, row.getString("image_link"));

storeValues.put(RATING,row.getString("rating"));

storeValues.put(COMMENT,row.getString("comment"));

storeValues.put(new_book_id,row.getString("new_book_id"));

}

Logcat

09-09 05:53:38.001: I/Choreographer(3377): Skipped 224 frames! The application may be doing too much work on its main thread.

09-09 05:53:38.032: D/dalvikvm(3377): GC_CONCURRENT freed 194K, 11% free 2979K/3320K, paused 70ms+16ms, total 145ms

09-09 05:53:38.041: D/dalvikvm(3377): WAIT_FOR_CONCURRENT_GC blocked 17ms

09-09 05:53:38.401: D/dalvikvm(3377): GC_CONCURRENT freed 341K, 16% free 3049K/3592K, paused 71ms+93ms, total 216ms

09-09 05:53:38.401: D/dalvikvm(3377): WAIT_FOR_CONCURRENT_GC blocked 134ms

09-09 05:53:38.770: D/dalvikvm(3377): GC_CONCURRENT freed 190K, 11% free 3293K/3684K, paused 89ms+6ms, total 188ms

09-09 05:53:38.770: D/dalvikvm(3377): WAIT_FOR_CONCURRENT_GC blocked 27ms

09-09 05:53:39.171: D/dalvikvm(3377): GC_CONCURRENT freed 445K, 17% free 3296K/3928K, paused 70ms+101ms, total 232ms

09-09 05:53:39.171: D/dalvikvm(3377): WAIT_FOR_CONCURRENT_GC blocked 15ms

09-09 05:53:39.840: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:40.041: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:40.100: I/Choreographer(3377): Skipped 56 frames! The application may be doing too much work on its main thread.

09-09 05:53:40.131: I/Choreographer(290): Skipped 41 frames! The application may be doing too much work on its main thread.

09-09 05:53:40.210: I/Choreographer(290): Skipped 30 frames! The application may be doing too much work on its main thread.

09-09 05:53:40.890: I/Choreographer(290): Skipped 50 frames! The application may be doing too much work on its main thread.

09-09 05:53:41.901: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:41.911: I/Choreographer(3377): Skipped 50 frames! The application may be doing too much work on its main thread.

09-09 05:53:41.991: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:42.065: I/Choreographer(3377): Skipped 45 frames! The application may be doing too much work on its main thread.

09-09 05:53:42.210: I/Choreographer(290): Skipped 55 frames! The application may be doing too much work on its main thread.

09-09 05:53:42.855: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:43.951: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:44.111: I/Choreographer(290): Skipped 53 frames! The application may be doing too much work on its main thread.

09-09 05:53:44.780: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:46.019: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:46.050: I/Choreographer(290): Skipped 68 frames! The application may be doing too much work on its main thread.

09-09 05:53:46.080: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:46.905: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:46.997: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:48.102: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:48.130: I/Choreographer(290): Skipped 66 frames! The application may be doing too much work on its main thread.

09-09 05:53:48.151: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:49.015: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:50.186: I/Choreographer(3377): Skipped 44 frames! The application may be doing too much work on its main thread.

09-09 05:53:50.221: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:50.221: I/Choreographer(290): Skipped 69 frames! The application may be doing too much work on its main thread.

09-09 05:53:51.106: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:52.210: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:53.013: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:54.252: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:54.281: I/Choreographer(290): Skipped 67 frames! The application may be doing too much work on its main thread.

09-09 05:53:54.290: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:55.173: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:56.351: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:56.381: I/Choreographer(290): Skipped 68 frames! The application may be doing too much work on its main thread.

09-09 05:53:56.410: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:56.601: I/Choreographer(290): Skipped 58 frames! The application may be doing too much work on its main thread.

09-09 05:53:57.233: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:58.422: I/Choreographer(3377): Skipped 44 frames! The application may be doing too much work on its main thread.

09-09 05:53:58.461: I/Choreographer(290): Skipped 68 frames! The application may be doing too much work on its main thread.

09-09 05:53:58.461: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:53:58.627: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:53:59.280: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:00.113: I/Choreographer(353): Skipped 45 frames! The application may be doing too much work on its main thread.

09-09 05:54:00.473: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:00.473: I/Choreographer(3377): Skipped 46 frames! The application may be doing too much work on its main thread.

09-09 05:54:00.520: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:00.610: I/Choreographer(290): Skipped 51 frames! The application may be doing too much work on its main thread.

09-09 05:54:00.610: I/Choreographer(3377): Skipped 53 frames! The application may be doing too much work on its main thread.

09-09 05:54:01.360: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:02.501: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:03.360: I/Choreographer(290): Skipped 50 frames! The application may be doing too much work on its main thread.

09-09 05:54:03.448: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:04.621: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:04.661: I/Choreographer(290): Skipped 69 frames! The application may be doing too much work on its main thread.

09-09 05:54:04.671: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:05.502: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:06.679: I/Choreographer(3377): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:06.711: I/Choreographer(290): Skipped 66 frames! The application may be doing too much work on its main thread.

09-09 05:54:06.740: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:06.881: I/Choreographer(290): Skipped 57 frames! The application may be doing too much work on its main thread.

09-09 05:54:07.568: I/Choreographer(290): Skipped 44 frames! The application may be doing too much work on its main thread.

09-09 05:54:07.661: I/Choreographer(290): Skipped 44 frames! The application may be doing too much work on its main thread.

09-09 05:54:08.741: I/Choreographer(290): Skipped 34 frames! The application may be doing too much work on its main thread.

09-09 05:54:08.770: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:08.961: D/dalvikvm(3377): GC_CONCURRENT freed 326K, 15% free 3362K/3920K, paused 5ms+7ms, total 282ms

09-09 05:54:10.784: I/Choreographer(3377): Skipped 44 frames! The application may be doing too much work on its main thread.

09-09 05:54:10.810: I/Choreographer(290): Skipped 66 frames! The application may be doing too much work on its main thread.

09-09 05:54:10.821: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:11.629: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:12.840: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property

09-09 05:54:12.980: I/Choreographer(290): Skipped 55 frames! The application may be doing too much work on its main thread.

09-09 05:54:13.700: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:14.936: I/Choreographer(290): Skipped 43 frames! The application may be doing too much work on its main thread.

09-09 05:54:15.010: I/Choreographer(290): Skipped 45 frames! The application may be doing too much work on its main thread.

Thanks in advance.

网友答案:

To parse the json. try the below

You have this

data1 = row.getJSONArray("comment_array");

for(int k=0;k<data1.length();k++){
    Toast.makeText(getApplicationContext(), "hellow"+k, Toast.LENGTH_SHORT).show();
}

But where are you parsing the json array. All you are doing is display a toast.

    try
    {
      JSONObject jsonobject = new JSONObject(jsonresult);
      JSONArray jarray = (JSONArray) jsonobject.getJSONArray("message"); 
      for(int i=0;i<jarray.length();i++)
      {
             JSONObject jb = (JSONObject)jarray.get(i);
             String bookid =  jb.getString("new_book_id");  
             JSONArray comment = (JSONArray)jb.getJSONArray("comment_array");
             for(int j=0;j<comment.length();j++)
             {
             JSONObject jb2 = (JSONObject)comment.get(j);
             String com = jb2.getString("com");
             String name = jb2.getString("name");
             Log.i("name is",name);
             }    
      }
   }catch(Exception e)
   {

   }
分享给朋友:
您可能感兴趣的文章:
随机阅读: