在JSON响应中解析数据

时间:2010-09-30 09:52:19

标签: android json parsing

这里我发布了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"
}
来自Category Tag的

。 任何人都可以帮助我,我能够从结果标签解析。 这是我的代码:

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 个答案:

答案 0 :(得分:1)

这是应该帮助你的site: - )