英文:
Valid JSON throwing JSONArray[0] is not a JSONObject
问题
我已经验证过的有效JSON,由JSONLint和JSON格式化和验证器验证过。我已经看到了其他的Stack Overflow帖子,比如:
提取JSON的代码如下:
JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray("advance_purchases");
int i = 0;
while (i < jsonADSArray.length()) {
JSONObject obj = jsonADSArray.getJSONObject(i);
}
以下这行代码:
JSONObject obj = jsonADSArray.getJSONObject(i);
抛出了错误:
JSONArray[0]不是JSONObject
其中strData是通过REST POST传入的:
{
"advance_purchases": [
"{ \"_id\":40, \"cancel_date\":1585760255892, \"cancel_user\":\"FACTORY\", \"create_date\":1585760206434, \"create_user\":\"FACTORY\", \"purchase_date\":1585918800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":53, \"cancel_date\":1585921300976, \"cancel_user\":\"Shipped\", \"create_date\":1585914517011, \"create_user\":\"FACTORY\", \"purchase_date\":1585918800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
...
]
}
还有:
jsonADSArray.length()
返回21
既然JSON已经经过验证,为什么代码会抛出错误?
英文:
I have valid JSON as validated by JSONLint and JSON Formatter and Validator. I have seen the other posts on SO like:
The code to extract the JSON is such:
JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray("advance_purchases");
int i = 0;
while(i < jsonADSArray.length()){
JSONObject obj = jsonADSArray.getJSONObject(i);
}
The line:
JSONObject obj = jsonADSArray.getJSONObject(i);
Throws the error:
JSONArray[0] is not a JSONObject
where strData passed in using REST POST:
{
"advance_purchases": [
"{ \"_id\":40, \"cancel_date\":1585760255892, \"cancel_user\":\"FACTORY\", \"create_date\":1585760206434, \"create_user\":\"FACTORY\", \"purchase_date\":1585918800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":53, \"cancel_date\":1585921300976, \"cancel_user\":\"Shipped\", \"create_date\":1585914517011, \"create_user\":\"FACTORY\", \"purchase_date\":1585918800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":54, \"cancel_date\":1585945994083, \"cancel_user\":\"Shipped\", \"create_date\":1585914517021, \"create_user\":\"FACTORY\", \"purchase_date\":1585929600000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":41, \"cancel_date\":1585760255920, \"cancel_user\":\"FACTORY\", \"create_date\":1585760207083, \"create_user\":\"FACTORY\", \"purchase_date\":1586005200000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":42, \"cancel_date\":1585760255932, \"cancel_user\":\"FACTORY\", \"create_date\":1585760207093, \"create_user\":\"FACTORY\", \"purchase_date\":1586016000000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":56, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585921279663, \"create_user\":\"FACTORY\", \"purchase_date\":1586005200000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":57, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585921279685, \"create_user\":\"FACTORY\", \"purchase_date\":1586016000000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":43, \"cancel_date\":1585861077997, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585760207724, \"create_user\":\"FACTORY\", \"purchase_date\":1586091600000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":44, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585860099795, \"create_user\":\"FACTORY\", \"purchase_date\":1586102400000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":55, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585914513604, \"create_user\":\"FACTORY\", \"purchase_date\":1586091600000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":45, \"cancel_date\":1585861078013, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585860109341, \"create_user\":\"FACTORY\", \"purchase_date\":1586178000000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":46, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585860109353, \"create_user\":\"FACTORY\", \"purchase_date\":1586188800000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":58, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585921275924, \"create_user\":\"FACTORY\", \"purchase_date\":1586178000000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":47, \"cancel_date\":1585861078023, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585860107142, \"create_user\":\"FACTORY\", \"purchase_date\":1586264400000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":48, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585860107157, \"create_user\":\"FACTORY\", \"purchase_date\":1586275200000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":59, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585921275944, \"create_user\":\"FACTORY\", \"purchase_date\":1586264400000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":49, \"cancel_date\":1585861078031, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585860105853, \"create_user\":\"FACTORY\", \"purchase_date\":1586350800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":50, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585860105865, \"create_user\":\"FACTORY\", \"purchase_date\":1586361600000, \"purchase_id\":729, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":60, \"cancel_date\":0, \"cancel_user\":\"\", \"create_date\":1585921275964, \"create_user\":\"FACTORY\", \"purchase_date\":1586350800000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":51, \"cancel_date\":1585861078040, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585860137954, \"create_user\":\"FACTORY\", \"purchase_date\":1586437200000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }",
"{ \"_id\":52, \"cancel_date\":1585861078049, \"cancel_user\":\"Purchase Changed\", \"create_date\":1585860138874, \"create_user\":\"FACTORY\", \"purchase_date\":1586523600000, \"purchase_id\":728, \"rejected\":0, \"remote_id\":-1, \"synced\":0 }"
]
}
And
jsonADSArray.length()
returns 21
> Since the JSON has been validated, why is the code throwing an error?
答案1
得分: 1
以下是您要翻译的内容:
这确实是一个有效的 JSON。然而,"{ \"_id\":40, ... , \"synced\":0 }"
不是一个 object
,而是一个 string
。(注意开头和结尾处的 "
)
首先尝试对其进行反序列化:
JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray("advance_purchases");
int i = 0;
while(i < jsonADSArray.length()){
String value = jsonADSArray.getString(i);
JSONObject obj = new JSONObject(value);
}
英文:
It is indeed a valid JSON. However "{ \"_id\":40, ... , \"synced\":0 }"
is not an object
it is a string
. (Note the "
at the start and end)
Try to deserialize it first:
JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray("advance_purchases");
int i = 0;
while(i < jsonADSArray.length()){
String value = jsonADSArray.getString(i);
JSONObject obj = new JSONObject(value);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论