简体   繁体   中英

Android: Parsing JSONArray of a JSONObject but still get syntax Error

i've read alot about parsing json in android and i don't know where i missed because i thinks that i do it right but it's keep showing me error

Here is my JSON:

{
    "categories": [
        {
            "id": 1,
            "category_name": "Kiehn, Kohler and Russel",
            "created_at": "2018-08-29 05:35:50",
            "updated_at": "2018-08-29 05:35:50"
        },
        {
            "id": 2,
            "category_name": "Flatley PLC",
            "created_at": "2018-08-29 05:35:50",
            "updated_at": "2018-08-29 05:35:50"
        }]
}

And here is my handle in android

try {
               ArrayList<TagModel> tagdata = new ArrayList<>();
               JSONObject object = new JSONObject(response);                   
               //Category
               ArrayList<CategoryModel> catedata = new ArrayList<>();
               JSONArray catearray = object.getJSONArray("categories");
               for (int i = 0; i < catearray.length(); i++) {
                  JSONObject product = catearray.getJSONObject(i);
                  CategoryModel recommend_model = new CategoryModel();
                  recommend_model.setCategoryName(product.getString("category_name"));
                  recommend_model.setCategoryId(String.valueOf(product.getInt("id")));
                  catedata.add(recommend_model);
               }                
            } catch (JSONException e) {
               e.printStackTrace();

            }

The Error keep telling the same issue that is:

at org.json.JSONTokener.syntaxError(JSONTokener.java:449) at org.json.JSONTokener.readObject(JSONTokener.java:393) at org.json.JSONTokener.nextValue(JSONTokener.java:100) at org.json.JSONObject.(JSONObject.java:159) at org.json.JSONObject.(JSONObject.java:176) at kh.com.example.sopheak_pc.CenturyRecipe.Data.DataClass$1.onResponse(DataClass.java:44) 08-30 06:47:37.283 22712-22712/kh.com.example.sopheak_pc.CenturyRecipe W/System.err: at kh.com.example.sopheak_pc.CenturyRecipe.Data.DataClass$1.onResponse(DataClass.java:38)

Here is the full log message: 记录错误

and when i debug, the error start from JSONObject object = new JSONObject(response);

and it's going to

 catch (JSONException e) {
               e.printStackTrace();
            }

Sorry if it's a bit messy because i want to show everywhere that relate to the error Thanks you!!!

i've just found my error inside string json and here what it's look like 这是我发现错误的地方

As @Andreas and @Sam Littlefair mention is my json contain "," inside the string so i clean up my data and enter simple data and it's working fine now! Thanks you 2 of you!

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