有效的 JSON 抛出 JSONArray[0] 不是 JSONObject。

huangapple go评论93阅读模式
英文:

Valid JSON throwing JSONArray[0] is not a JSONObject

问题

我已经验证过的有效JSON,由JSONLintJSON格式化和验证器验证过。我已经看到了其他的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(&quot;advance_purchases&quot;);
int i = 0;
while(i &lt; 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:

{
&quot;advance_purchases&quot;: [
&quot;{ \&quot;_id\&quot;:40, \&quot;cancel_date\&quot;:1585760255892, \&quot;cancel_user\&quot;:\&quot;FACTORY\&quot;, \&quot;create_date\&quot;:1585760206434, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1585918800000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:53, \&quot;cancel_date\&quot;:1585921300976, \&quot;cancel_user\&quot;:\&quot;Shipped\&quot;, \&quot;create_date\&quot;:1585914517011, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1585918800000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:54, \&quot;cancel_date\&quot;:1585945994083, \&quot;cancel_user\&quot;:\&quot;Shipped\&quot;, \&quot;create_date\&quot;:1585914517021, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1585929600000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:41, \&quot;cancel_date\&quot;:1585760255920, \&quot;cancel_user\&quot;:\&quot;FACTORY\&quot;, \&quot;create_date\&quot;:1585760207083, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586005200000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:42, \&quot;cancel_date\&quot;:1585760255932, \&quot;cancel_user\&quot;:\&quot;FACTORY\&quot;, \&quot;create_date\&quot;:1585760207093, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586016000000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:56, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585921279663, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586005200000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:57, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585921279685, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586016000000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:43, \&quot;cancel_date\&quot;:1585861077997, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585760207724, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586091600000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:44, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585860099795, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586102400000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:55, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585914513604, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586091600000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:45, \&quot;cancel_date\&quot;:1585861078013, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585860109341, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586178000000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:46, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585860109353, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586188800000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:58, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585921275924, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586178000000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:47, \&quot;cancel_date\&quot;:1585861078023, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585860107142, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586264400000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:48, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585860107157, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586275200000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:59, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585921275944, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586264400000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:49, \&quot;cancel_date\&quot;:1585861078031, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585860105853, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586350800000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:50, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585860105865, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586361600000, \&quot;purchase_id\&quot;:729, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:60, \&quot;cancel_date\&quot;:0, \&quot;cancel_user\&quot;:\&quot;\&quot;, \&quot;create_date\&quot;:1585921275964, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586350800000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:51, \&quot;cancel_date\&quot;:1585861078040, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585860137954, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586437200000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;,
&quot;{ \&quot;_id\&quot;:52, \&quot;cancel_date\&quot;:1585861078049, \&quot;cancel_user\&quot;:\&quot;Purchase Changed\&quot;, \&quot;create_date\&quot;:1585860138874, \&quot;create_user\&quot;:\&quot;FACTORY\&quot;, \&quot;purchase_date\&quot;:1586523600000, \&quot;purchase_id\&quot;:728, \&quot;rejected\&quot;:0, \&quot;remote_id\&quot;:-1, \&quot;synced\&quot;:0 }&quot;
]
}

And

jsonADSArray.length()

returns 21

> Since the JSON has been validated, why is the code throwing an error?

答案1

得分: 1

以下是您要翻译的内容:

这确实是一个有效的 JSON。然而,&quot;{ \&quot;_id\&quot;:40, ... , \&quot;synced\&quot;:0 }&quot; 不是一个 object,而是一个 string。(注意开头和结尾处的 &quot;

首先尝试对其进行反序列化:

JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray(&quot;advance_purchases&quot;);
int i = 0;
while(i &lt; jsonADSArray.length()){
    String value = jsonADSArray.getString(i);
    JSONObject obj = new JSONObject(value);
}
英文:

It is indeed a valid JSON. However &quot;{ \&quot;_id\&quot;:40, ... , \&quot;synced\&quot;:0 }&quot; is not an object it is a string. (Note the &quot; at the start and end)

Try to deserialize it first:

JSONObject jsonObject = new JSONObject(strData);
JSONArray jsonADSArray = jsonObject.getJSONArray(&quot;advance_purchases&quot;);
int i = 0;
while(i &lt; jsonADSArray.length()){
    String value = jsonADSArray.getString(i);
    JSONObject obj = new JSONObject(value);
}

huangapple
  • 本文由 发表于 2020年4月4日 06:37:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定