从JSONarray获取特定值

时间:2015-09-27 00:09:10

标签: java android json

在我的一个类的android项目中,我有一个JSONarray,里面有这样的值。

{"imgUrl":"\\assets\\images\\projectpics\\normalt\\Edited_front.jpg","Name":"Normal T-shirt","View":"Unread","Status":"Cart","Quantity":"10","Date_Sub":"2015-09-26","Comment":null,"Customer_ID":"12","Order_ID":"21","Product_ID":"1","Date_Del":null}

我需要将分配给Customer_ID的值转换为字符串

我该怎么办?

3 个答案:

答案 0 :(得分:0)

有几个用于读取和解释Json文件的库(例如Android Developer库中的JsonReader:http://developer.android.com/reference/android/util/JsonReader.html

也许你应该看一下!使用这些库,您可以遍历json元素到您需要的深度和元素!

答案 1 :(得分:0)

如果您有名为jsonArray的JsonArray。

for(int i=0; i<jsonArray.length(); i++) {
   JSONObject item = jsonArray.getJSONObject(i);  //gets the ith Json object of JSONArray
   String customerId = item.getString("Customer_ID");
}

答案 2 :(得分:0)

    try {
        String jsonString =
                "{\"imgUrl\":\"\\\\assets\\\\images\\\\projectpics\\\\normalt\\\\Edited_front.jpg\",\"Name\":\"Normal T-shirt\",\"View\":\"Unread\",\"Status\":\"Cart\",\"Quantity\":\"10\",\"Date_Sub\":\"2015-09-26\",\"Comment\":null,\"Customer_ID\":\"12\",\"Order_ID\":\"21\",\"Product_ID\":\"1\",\"Date_Del\":null}\n";
        JSONObject jsonObject = new JSONObject(jsonString);
        String customerID = jsonObject.getString("Customer_ID");
        Log.d("JSON", "Customer_ID: " + customerID);
    } catch (JSONException e) {
        e.printStackTrace();
    }

输出:

    D/JSON(1165): Customer_ID: 12