简体   繁体   English

Android:解析JSONObject的JSONArray,但仍获取语法错误

[英]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 我已经读过很多关于在an​​droid中解析json的信息,但我不知道我错过了什么,因为我认为我做对了,但是它一直向我显示错误

Here is my JSON: 这是我的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 这是我在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) 在org.json.JSONTokener.nextValue(JSONTokener.java:100)处的org.json.JSONTokener.readObject(JSONTokener.java:393)处的org.json.JSONTokener.syntaxError(JSONTokener.java:449)处。 org.json.JSONObject.JSON对象。(JSONObject.java:176)处的JSONObject。(JSONObject.java:159).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); 当我调试时,错误从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 我刚刚在字符串json中发现了我的错误,这看起来像什么 这是我发现错误的地方

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! 由于@Andreas和@Sam Littlefair提到的是我的json在字符串中包含“,”,因此我清理了数据并输入简单数据,现在可以正常工作了! Thanks you 2 of you! 谢谢你们两个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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