JSONArray.length()在Android Studio中返回错误的长度

时间:2021-09-07 15:01:29

I am parsing a JSON string that has an array of 5 JSON objects. Though, when I call the JSONArray .length() method, it returns 6 instead of 5. Any clue why this is so? Thanks.

我正在解析一个包含5个JSON对象数组的JSON字符串。虽然,当我调用JSONArray .length()方法时,它返回6而不是5.任何线索为什么会这样?谢谢。

Here is the JSON string:

这是JSON字符串:

String jsonStr = 
   "["
                + "{"
                + " \"deptNbr\": 1, "
                + " \"catgNbr\": 120,"
                + " \"catgDesc\": \"COLD CEREAL\","
                + " \"modSect\": 13,"
                + " \"locationId\": \"0001\"," 
                + " \"upc\": \"2200001654\"," 
                + " \"primeItemNbr\": 553279324,"
                + " \"itemNbr\": \"1\","
                + " \"itemDesc\": \"LIFE CINN\"," 
                + " \"price\": 1.98,"
                + " \"horizontalFacings\": 1,"
                + " \"verticalFacings\": 1,"
                + " \"capacity\": 35,"
                + " \"shelf\": 2,"
                + " \"productHeight\": 10.43,"
                + " \"productWidth\": 7.5,"
                + " \"productDepth\": 2.5,"
                + " \"xCoord\": 0"
                + " },"
                + " {"
                + " \"deptNbr\": 1,"
                + " \"catgNbr\": 120,"
                + " \"catgDesc\": \"COLD CEREAL\"," 
                + "\"modSect\": 13,"
                + "\"locationId\": \"0002\","
                + "\"upc\": \"4000032968\","
                + "\"primeItemNbr\": 130958,"
                + "\"itemNbr\": \"130958\","
                + "\"itemDesc\": \"HONEY NUT CHERRIOS\","
                + "\"price\": 1.98,"
                + "\"horizontalFacings\": 2,"
                + "\"verticalFacings\": 1,"
                + "\"capacity\": 20,"
                + "\"shelf\": 2,"
                + "\"productHeight\": 11.25,"
                + "\"productWidth\": 7.67,"
                + "\"productDepth\": 2,"
                + "\"xCoord\": 8.5"
                + "},"
                + "{"
                + "\"deptNbr\": 1,"
                + "\"catgNbr\": 120,"
                + "\"catgDesc\": \"COLD CEREAL\","
                + "\"modSect\": 13,"
                + "\"locationId\": \"0007\","
                + "\"upc\": \"79051314000\","
                + "\"primeItemNbr\": 137063,"
                + "\"itemNbr\": \"4\","
                + "\"itemDesc\": \"CINN TOAST CRUNCH\","
                + "\"price\": 3.32,"
                + "\"horizontalFacings\": 1,"
                + "\"verticalFacings\": 1,"
                + "\"capacity\": 24,"
                + "\"shelf\": 1,"
                + "\"productHeight\": 10.25,"
                + "\"productWidth\": 7.67,"
                + "\"productDepth\": 2,"
                + "\"xCoord\": 0"
                + " },"
                + " {"
                + "\"deptNbr\": 1,"
                + "\"catgNbr\": 120,"
                + "\"catgDesc\": \"COLD CEREAL\","
                + "\"modSect\": 13,"
                + "\"locationId\": \"0008\","
                + "\"upc\": \"2800078552\","
                + "\"primeItemNbr\": 138779,"
                + "\"itemNbr\": \"5\","
                + "\"itemDesc\": \"STAR WARS\","
                + "\"price\": 4.28,"
                + "\"horizontalFacings\": 1,"
                + "\"verticalFacings\": 1,"
                + "\"capacity\": 21,"
                + "\"shelf\": 1,"
                + "\"productHeight\": 10.5,"
                + "\"productWidth\": 7.67,"
                + "\"productDepth\": 2,"
                + "\"xCoord\": 8.5"
                + "},"
                + "{"
                + "\"deptNbr\": 1,"
                + "\"catgNbr\": 120,"
                + "\"catgDesc\": \"COLD CEREAL\","
                + "\"modSect\": 13,"
                + "\"locationId\": \"0009\","
                + "\"upc\": \"7339000459\","
                + "\"primeItemNbr\": 194231,"
                + "\"itemNbr\": \"6\",  "
                + "\"itemDesc\": \"LUCKY CHARMS\", "
                + "\"price\": 2.28, "
                + "\"horizontalFacings\": 1, "
                + "\"verticalFacings\": 1, "
                + "\"capacity\": 24, "
                + "\"shelf\": 1, "
                + "\"productHeight\": 11.25, "
                + "\"productWidth\": 7.67, "
                + "\"productDepth\": 2, "
                + "\"xCoord\": 17 "
                + " }," +
 "]";

Here is where I create the JSONArray object and call the length() method:

这是我创建JSONArray对象并调用length()方法的地方:

try{
   JSONArray cerealArray = new JSONArray(jsonStr);
   Log.d(LOGTAG,"cerealArray.length(): " + cerealArray.length());
}catch(JSONException e){
   Log.e("JSONException: ", "failed to read jsonString");
}

In my logcat:

在我的logcat中:

D/ImageTargetRenderer: planogramArray.length(): 6

1 个解决方案

#1


0  

That's because of the last ',' in your JSON

那是因为JSON中的最后一个','

                + " }," +

remove that and it should be ok

删除它,它应该没问题

#1


0  

That's because of the last ',' in your JSON

那是因为JSON中的最后一个','

                + " }," +

remove that and it should be ok

删除它,它应该没问题