开发者

Parsing the Data in JSON Response

开发者 https://www.devze.com 2023-01-18 17:40 出处:网络
Here I am posting the Json Response: { \"ResultSet\": { \"Result\": [{ \"Phone\": \"(650) 473-9999\", \"Distance\": \"2.59\",

Here I am posting the Json Response:

{
  "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"
        }]
      },

Now I just want to parse the data

{
  "content": "Salad Restaurants",
  "id": "96926225"
}, {
  "content": "Vegetarian Restaurants",
  "id": "96926239"
}, {
  "content": "Pizza",
  "id": "96926243"
}, {
  "c开发者_如何学Content": "Wine",
  "id": "96926864"
}, {
  "content": "Alcoholic Beverages",
  "id": "96929810"
}

from the Category Tag. Can anybody please help me, I am able to parse from the Result Tag. Here is my code :

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


This is the site that ought to help you :-)

0

精彩评论

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