2010-09-30 89 views
0

這裏我張貼的JSON響應解析數據:在JSON響應

{ 
    "ResultSet": { 
    "Result": [{ 
     "Phone": "(650) 473-9999", 
     "Distance": "2.59", 
     "MapUrl": "http://maps.yahoo.com/maps_result?q1=441+Emerson+St+Palo+Alto+CAgid1=28734629", 
     "Categories": { 
     "Category": [{ 
      "content": "Salad Restaurants", 
      "id": "96926225" 
     }, { 
      "content": "Vegetarian Restaurants", 
      "id": "96926239" 
     }, { 
      "content": "Pizza", 
      "id": "96926243" 
     }, { 
      "content": "Wine", 
      "id": "96926864" 
     }, { 
      "content": "Alcoholic Beverages", 
      "id": "96929810" 
     }] 
     }, 

現在我只想分析數據

{ 
    "content": "Salad Restaurants", 
    "id": "96926225" 
}, { 
    "content": "Vegetarian Restaurants", 
    "id": "96926239" 
}, { 
    "content": "Pizza", 
    "id": "96926243" 
}, { 
    "content": "Wine", 
    "id": "96926864" 
}, { 
    "content": "Alcoholic Beverages", 
    "id": "96929810" 
} 

從類別標籤。 任何人都可以幫助我,我可以從結果標籤解析。 這裏是我的代碼:

private String connect(String url) { 
    // Create the httpclient 
    HttpClient httpclient = new DefaultHttpClient(); 

    //Prepare a request Object 
    HttpGet httpget = new HttpGet(url); 

    //Response Declaration 
    HttpResponse response; 

    //Return String Declaration 
    String returnString = null; 


    try { 

    // Open the webpage. 
    response = httpclient.execute(httpget); 

    if (response.getStatusLine().getStatusCode() == 200) { 
     // Connection was established. Get the content. 
     HttpEntity entity = response.getEntity(); 
     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 
     if (entity != null) { 
     // A Simple JSON Response Read 
     InputStream instream = entity.getContent(); 


     // Load the requested page converted to a string into a JSONObject. 
     JSONObject myAwway = new JSONObject(convertStreamToString(instream)); 
     Log.e(DATA, "myData"); 
     System.out.println("Response : - - " + myAwway); 
     // Get the query value' 
     // String phone = myAwway.getString("Phone"); 
/* String resultset = myAwway.getString("ResultSet"); 
         JSONObject temp = new JSONObject(resultset); 
         String string = temp.getString("Result"); 
         JSONArray result = new JSONArray(string); 
         */ 

     String resultset = myAwway.getString("ResultSet"); 
     JSONObject temp = new JSONObject(resultset); 
     String string = temp.getString("Category"); 
     JSONArray result = new JSONArray(string); 

/* JSONObject temp1 = new JSONObject(string); 
         String string1 = temp1.getString("Category"); 
         JSONArray result1 = new JSONArray(string1); 
         */ 

     //JSONArray result = myAwway.getJSONArray("Result").getString(resultset); 
     // JSONArray result = myAwway.getJSONArray("Result"); 
     // Build the return string. 
     returnString = "DataFound: " + result.length(); 
     for (int i = 0; i < result.length(); i++) { 

      //returnString += "\n\t" + result.getString(i); 
      returnString += "\n\t" + result.getJSONObject(i).getString("Phone"); 
      returnString += "\n\t" + result.getJSONObject(i).getString("Distance"); 
      returnString += "\n\t" + result.getJSONObject(i).getString("BusinessUrl"); 
      returnString += "\n\t" + result.getJSONObject(i).getString("Rating"); 
/*for (int j = 0; j < result1.length(); j++) 
          { 
           returnString += "\n\t" + result1.getJSONObject(j).getString("Category"); 
          } */ 

      System.out.println("JsonOutPut:---" + result); 
     } 



     // Close the stream. 
     instream.close(); 
     } 
    } else { 
     // code here for a response other than 200. A response 200 means the webpage was ok 
     // Other codes include 404 - not found, 301 - redirect etc... 
     // Display the response line. 
     returnString = "Unable to load page - " + response.getStatusLine(); 
    } 
    } catch (IOException ex) { 
    // thrown by line 80 - getContent(); 
    // Connection was not established 
    returnString = "Connection failed; " + ex.getMessage(); 
    } catch (JSONException ex) { 
    // JSON errors 
    returnString = "JSON failed; " + ex.getMessage(); 
    } 

    return returnString; 
} 

回答

1

這是site這應該可以幫助你:-)

+0

+1俏皮的小工具。 – 2010-09-30 10:21:10