简体   繁体   中英

Android json parsing error

My json data is given below:

[
  {"Category_Id":"C1 ","Category_Name":"breakfast"},
  {"Category_Id":"C2 ","Category_Name":"appetizers & snacks"},
  {"Category_Id":"C3 ","Category_Name":"sandwiches & more"},
  {"Category_Id":"C4 ","Category_Name":"burgers & wraps"},
  {"Category_Id":"C5 ","Category_Name":"salads"},
  {"Category_Id":"C6 ","Category_Name":"soup"},
  {"Category_Id":"C7 ","Category_Name":"pizza & pasta"},
  {"Category_Id":"C8 ","Category_Name":"kids menu"},
  {"Category_Id":"C9 ","Category_Name":"main dishes"},
  {"Category_Id":"C10 ","Category_Name":"oriental cuisine"},
  {"Category_Id":"C11 ","Category_Name":"desserts"},
  {"Category_Id":"C12 ","Category_Name":"shakes & smoothies"},
  {"Category_Id":"C13 ","Category_Name":"drinks"},
  {"Category_Id":"C14","Category_Name":"333"}
]

how could i parse these data and set the text in button dynamically.

If you get json parser error just check whether your json string is correct or not by checking it in online in any json parsing website like:-

http://jsonlint.com/

OR

http://jsonviewer.stack.hu/

String str ="[{\"Category_Id\":\"C1 \",\"Category_Name\":\"breakfast\"},{\"Category_Id\":\"C2 \",\"Category_Name\":\"appetizers & snacks\"},{\"Category_Id\":\"C3 \",\"Category_Name\":\"sandwiches & more\"},{\"Category_Id\":\"C4 \",\"Category_Name\":\"burgers & wraps\"},{\"Category_Id\":\"C5 \",\"Category_Name\":\"salads\"},{\"Category_Id\":\"C6 \",\"Category_Name\":\"soup\"},{\"Category_Id\":\"C7 \",\"Category_Name\":\"pizza & pasta\"},{\"Category_Id\":\"C8 \",\"Category_Name\":\"kids menu\"},{\"Category_Id\":\"C9 \",\"Category_Name\":\"main dishes\"},{\"Category_Id\":\"C10 \",\"Category_Name\":\"oriental cuisine\"},{\"Category_Id\":\"C11 \",\"Category_Name\":\"desserts\"},{\"Category_Id\":\"C12 \",\"Category_Name\":\"shakes & smoothies\"},{\"Category_Id\":\"C13 \",\"Category_Name\":\"drinks\"},{\"Category_Id\":\"C14\",\"Category_Name\":\"333\"}]";
        JSONArray jarray;
        try{
            jarray = new JSONArray("");
            for(int i = 0; i < jarray.length(); i++)
            {
                String id = jarray.getJSONObject(i).getString("Category_Id");
                String name = jarray.getJSONObject(i).getString("Category_Name");
            }
        }catch(Exception e){}

For this all you need to do is a for loop to go through the Json response. Something like this should do the trick.

    JSONArray jsonArray;
    try{
        jsonArray = new JSONArray(myJsonResponse);
        for(int i = 0; i < jsonArray.length(); i++)
        {

JsonObject object = new JsonObject(jsonArray.getJsonObject(i));
            buttonTextString = object.getString("Category_Name);
        }
    } catch(Exception e) {}

You can parse JSONArray before then parse JSONObject to parse each element on your json data. And final setText for button each element you can parse

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