1

可能重复:
如何在 Android 中解析 JSON 对象

我有如下 JSON 对象.. 我如何解析对象?

{
  "0":
     {
       "productname":"Famous Amos Bite Size Chocolate Chip Cookies - 4 Pack",
       "imageurl":"http://ecx.images-amazon.com/images/I/513j-WyH1GL._SL160_.jpg",
       "producturl":"http://www.searchupc.com/rd.aspx?u=d%2bKvXQ%2fFIfa95xJ38QYLycSjbm5dt4dy3l4IYTYPM3agt4tefTNsMwzWkPWd9gCY%2fEnCdaGVMLsQD%2fO5ZUWbfJyqOuwIWqkLvouDyw5u7VWmda5dK2%2fRTmcAp3%2f1TImmZmtdaNauL74Lj8BkV0r15VeazeDf4Im4Nx%2f5TOuqBUnUXzeNkYrWvlLitV8FDFIkM77UIjZzYZqoQANt0PBNeqh94bzLqFRXpNYPyqc0fLDTHnA9TM2jsbaKVN23UA%2fH",
       "price":"5.95",
       "currency":"usd",
       "saleprice":"",
       "storename":"amazon.com"
     }
}
4

5 回答 5

3

下面的示例演示了如何解析 JSON。您将在此处获得关于 json 解析的非常清晰的解释。

JSONObject myjson = new JSONObject("put you json string here");
JSONArray the_json_array = myjson.getJSONArray("0");
int size = the_json_array.length();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = the_json_array.getJSONObject(i);           
    System.out.println((String) another_json_object.get("productname"));        
    System.out.println((String) another_json_object.get("imageurl"));       
    System.out.println((String) another_json_object.get("producturl"));
}
于 2012-08-28T10:24:35.330 回答
2

Android 提供了org.json 包,可用于将您的 String 解析为 JSONObject。

您可以使用这行代码来实现这一点:

JSONObject json = new JSONObject(myJsonString);
于 2012-08-28T10:22:37.577 回答
2

你必须使用JSONObject这个。使用以下代码 -

JSON解析器.java

public JSONObject getJSONFromUrl(String url)  // url is your json url
{

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    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();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

它将返回JsonObjectAnd,使用这个 jsonbobject 我们可以得到数组的结果。

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
    // Getting Array of Contacts
    contacts = json.getJSONArray(TAG_CONTACTS);

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

        String product_name = contacts.getString("productname");
        String image_url = contacts.getString("imageurl");
        .....
        .....
        String store_name = contacts.getString("storename");
    }
}catch(JSONException e) 
{
    e.printStackTrace();
}

有关更多信息,请参阅Android json 教程

于 2012-08-28T10:29:41.213 回答
1

有一个工作示例YatayatService使用json 库和自定义JsonParser.java使用apache http 库。实现方法是getAllRoutes()

String jsonString = JsonParser.parseJSON(URL);
try {
    JSONObject parentObject = new JSONObject(jsonString);
 } catch (JSONException e) {
    e.printStackTrace();
 }
于 2012-08-28T10:42:02.820 回答
1

试试这个代码

 // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    System.out.println("json is "+json);
    System.out.println("Length"+json.length());

    for (int i = 0; i < json.length(); i++) {
        try {
            String atObj = Integer.toString(i);
            System.out.println(atObj);
            JSONObject jObj = json.getJSONObject(atObj);
            System.out.println(jObj.getString("productname"));
            System.out.println(jObj.getString("imageurl"));
            System.out.println(jObj.getString("producturl"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
于 2012-08-28T11:49:13.290 回答