Adrian Adrian - 22 days ago 6
JSON Question

JSONArray.length() returns wrong length in Android Studio

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.

Here is the JSON string:

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:

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:

D/ImageTargetRenderer: planogramArray.length(): 6

Answer

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

                + " }," +

remove that and it should be ok