-1

這是JSON文件:如何達到所需點的輸出中的Android程序

{ 
    "response":{ 
     "code":1, 
     "message":"success" 
    }, 
    "data":{ 
     "updates":[ 
     { 
      "update_id":"67", 
      "date":"6 months ago", 
      "update_type":"7", 
      "update_id":"67", 
      "name":"ravi" 
     }, 
     { 
      "update_id":"68", 
      "date":"3 months ago", 
      "update_type":"5", 
      "update_id":"68", 
      "name":"paresh" 
     }, 
     { 
      "update_id":"69", 
      "date":"1 months ago", 
      "update_type":"6", 
      "update_id":"69", 
      "name":"sampath" 
     }, 
     { 
      "update_id":"62", 
      "date":"9 months ago", 
      "update_type":"6", 
      "update_id":"62", 
      "name":"raju" 
     } 
     ] 
    } 
} 

回答

1

試試這個,

String str_json = "your json string from query"; 

    try { 
     JSONObject topobj = new JSONObject(str_json); 
     JSONObject innerobj = topobj.getJSONObject("yourdate"); 

     String name = innerobj.getString("yourdata"); 
     String photo = innerobj.getString("yourdata"); 
     JSONArray cuisines = innerobj.getJSONArray("yourdata"); 
     //etc etc... 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
0
try { 
     HttpClient httpclient = getNewHttpClient(); 
     HttpGet httppost = new HttpGet(URL); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

    } catch(Exception e) { 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
    } 

    // convert response to string 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    } catch(Exception e) { 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 

    try { 
     Log.e("log_tag", "result" + result.toString()); 
     JSONObject json_data = new JSONObject(result); 
     String data = json_data.getString("Data"); 
     json_data = new JSONObject(data); 
     String updates = json_data.getString("updates"); 
     JSONArray json_data1 = new JSONArray(updates); 
     for(int i = 0; i < json_data1.length(); i++) { 
      json_data = json_data1.getJSONObject(i); 
      String update_id = json_data.getString("update_id"); 
      String date = json_data.getString("date"); 
     } 
    } catch(Exception e) { 

    }