简体   繁体   中英

Get the specific value from JSONarray

In my android project in a class I have a JSONarray in which has values like this.

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

I need to get the value assigned to Customer_ID into a string,

how can I do it?

There are several librarys for reading and interpreting Json files (eg JsonReader from the Android Developer library: http://developer.android.com/reference/android/util/JsonReader.html )

Maybe you should have a look at that! With those librarys you can iterate through the json elements to the depth and element you need!

If you have JsonArray named 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");
}
    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();
    }

output:

    D/JSON(1165): Customer_ID: 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM