JSONArray.length() returns Android Studio 中的长度错误
JSONArray.length() returns wrong length in Android Studio
我正在解析一个 JSON 字符串,它有一个包含 5 个 JSON 对象的数组。虽然,当我调用 JSONArray .length() 方法时,它 returns 6 而不是 5。任何线索为什么会这样?谢谢。
这是 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 "
+ " }," +
"]";
这里是我创建 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");
}
在我的 logcat:
D/ImageTargetRenderer: planogramArray.length(): 6
那是因为你的 JSON
中的最后一个 ','
+ " }," +
去掉那个应该就可以了
我正在解析一个 JSON 字符串,它有一个包含 5 个 JSON 对象的数组。虽然,当我调用 JSONArray .length() 方法时,它 returns 6 而不是 5。任何线索为什么会这样?谢谢。
这是 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 "
+ " }," +
"]";
这里是我创建 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");
}
在我的 logcat:
D/ImageTargetRenderer: planogramArray.length(): 6
那是因为你的 JSON
中的最后一个 ',' + " }," +
去掉那个应该就可以了