org.json.JSONException: JSONArray[0] is not a JSONObject – Java

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

org.json.JSONException: JSONArray[0] is not a JSONObject - Java

问题

public void Trial() throws JSONException {
    String json = "[[{\"appId\": \"MBSP\",\"askPrice\": 0,\"bidPrice\": 0,\"collectionDataSource\": \"ExternalTick\",\"collectionName\": \"FRM_MBS_TBA_FN_15Y_0.03_FWD0\",\"collectionObservationTime\": \"2020-09-21T17:47:59.703Z\",\"collectionType\": \"LIVE\",\"coupon\": 1.03,\"createdBy\": \"Test\",\"createdOn\": \"2020-09-21T17:47:59.703Z\",\"createdOnDate\": 0,\"forward\": 0,\"issuingAgency\": \"FF\",\"lastUpdated\": \"2020-09-21T17:47:59.703Z\",\"lastUpdatedBy\": \"string\",\"lastUpdatedDate\": 0,\"maturity\": ,\"midPrice\":0 ,\"mtaVersionNumber\": 0,\"settlementDate\": \"2020-09-21T17:47:59.703Z\"}]]";
    JSONArray jsonObj = new JSONArray(json);
    for (int i = 0; i < jsonObj.length(); i++) {
        JSONObject jsonobject = jsonObj.getJSONArray(i).getJSONObject(0);
        double Coupon = jsonobject.getDouble("coupon");
        System.out.println(Coupon);
    }
}
英文:

Hello I am trying to read and parse a JSON file, when I attempt to read it I got exception of =org.json.JSONException: JSONArray[0] is not a JSONObject. The JSON is shorten for sake of example. Provided will be my code,json and desired output.

Code:

public void Trial () throws JSONException {
		String json = &quot;[[{&quot;appId&quot;: &quot;MBSP&quot;,&quot;askPrice&quot;: 0,&quot;bidPrice&quot;: 0,&quot;collectionDataSource&quot;: &quot;ExternalTick&quot;,&quot;collectionName&quot;: &quot;FRM_MBS_TBA_FN_15Y_0.03_FWD0&quot;,&quot;collectionObservationTime&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,&quot;collectionType&quot;: &quot;LIVE&quot;,&quot;coupon&quot;: 1.03,&quot;createdBy&quot;: &quot;Test&quot;,&quot;createdOn&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,&quot;createdOnDate&quot;: 0,&quot;forward&quot;: 0,&quot;issuingAgency&quot;: &quot;FF&quot;,&quot;lastUpdated&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,&quot;lastUpdatedBy&quot;: &quot;string&quot;,&quot;lastUpdatedDate&quot;: 0,&quot;maturity&quot;: ,&quot;midPrice&quot;:0 ,&quot;mtaVersionNumber&quot;: 0,&quot;settlementDate&quot;: &quot;2020-09-21T17:47:59.703Z&quot;}]]
 &quot;;
		JSONArray jsonObj = new JSONArray(json);
		for (int i = 0; i &lt; jsonObj.length(); i++) {
		    JSONObject jsonobject = jsonObj.getJSONObject(i);
		    String Coupon = jsonobject.getString(&quot;Coupon&quot;);
		    System.out.println(Coupon);
		}
	}

JSON:

[[
  {
    &quot;appId&quot;: &quot;MBSP&quot;,
    &quot;askPrice&quot;: 0,
    &quot;bidPrice&quot;: 0,
    &quot;collectionDataSource&quot;: &quot;ExternalTick&quot;,
    &quot;collectionName&quot;: &quot;FRM_MBS_TBA_FN_15Y_0.03_FWD0&quot;,
    &quot;collectionObservationTime&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;collectionType&quot;: &quot;LIVE&quot;,
    &quot;coupon&quot;: 1.03,
    &quot;createdBy&quot;: &quot;Test&quot;,
    &quot;createdOn&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;createdOnDate&quot;: 0,
    &quot;forward&quot;: 0,
    &quot;issuingAgency&quot;: &quot;FF&quot;,
    &quot;lastUpdated&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;lastUpdatedBy&quot;: &quot;string&quot;,
    &quot;lastUpdatedDate&quot;: 0,
    &quot;maturity&quot;: ,
    &quot;midPrice&quot;:0 ,
    &quot;mtaVersionNumber&quot;: 0,
    &quot;settlementDate&quot;: &quot;2020-09-21T17:47:59.703Z&quot;
  }
]]

Wanted ooutput

 1.03

Any help would be appreciated.

答案1

得分: 2

以下是翻译好的内容:

有效的 JSON 应为:

[
  {
    "appId": "MBSP",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "FRM_MBS_TBA_FN_15Y_0.03_FWD0",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "FF",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": 0,
    "midPrice": 0,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  }
]

同时更新代码:

public class Sample {

    public static void main(String[] args) {
        String json = "[{\n" +
            "    \"appId\": \"MBSP\",\n" +
            "    \"askPrice\": 0,\n" +
            "    \"bidPrice\": 0,\n" +
            "    \"collectionDataSource\": \"ExternalTick\",\n" +
            "    \"collectionName\": \"FRM_MBS_TBA_FN_15Y_0.03_FWD0\",\n" +
            "    \"collectionObservationTime\": \"2020-09-21T17:47:59.703Z\",\n" +
            "    \"collectionType\": \"LIVE\",\n" +
            "    \"coupon\": 1.03,\n" +
            "    \"createdBy\": \"Test\",\n" +
            "    \"createdOn\": \"2020-09-21T17:47:59.703Z\",\n" +
            "    \"createdOnDate\": 0,\n" +
            "    \"forward\": 0,\n" +
            "    \"issuingAgency\": \"FF\",\n" +
            "    \"lastUpdated\": \"2020-09-21T17:47:59.703Z\",\n" +
            "    \"lastUpdatedBy\": \"string\",\n" +
            "    \"lastUpdatedDate\": 0,\n" +
            "    \"maturity\": 0,\n" +
            "    \"midPrice\": 0,\n" +
            "    \"mtaVersionNumber\": 0,\n" +
            "    \"settlementDate\": \"2020-09-21T17:47:59.703Z\"\n" +
            "}]";
        JSONArray jsonObj = new JSONArray(json);
        for (int i = 0; i < jsonObj.length(); i++) {
            JSONObject jsonobject = jsonObj.getJSONObject(i);
            double Coupon = jsonobject.getDouble("coupon");
            System.out.println(Coupon);
        }
    }

}

输出:

1.03
英文:

The valid JSON should be :

[
  {
    &quot;appId&quot;: &quot;MBSP&quot;,
    &quot;askPrice&quot;: 0,
    &quot;bidPrice&quot;: 0,
    &quot;collectionDataSource&quot;: &quot;ExternalTick&quot;,
    &quot;collectionName&quot;: &quot;FRM_MBS_TBA_FN_15Y_0.03_FWD0&quot;,
    &quot;collectionObservationTime&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;collectionType&quot;: &quot;LIVE&quot;,
    &quot;coupon&quot;: 1.03,
    &quot;createdBy&quot;: &quot;Test&quot;,
    &quot;createdOn&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;createdOnDate&quot;: 0,
    &quot;forward&quot;: 0,
    &quot;issuingAgency&quot;: &quot;FF&quot;,
    &quot;lastUpdated&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
    &quot;lastUpdatedBy&quot;: &quot;string&quot;,
    &quot;lastUpdatedDate&quot;: 0,
    &quot;maturity&quot;: 0,
    &quot;midPrice&quot;:0 ,
    &quot;mtaVersionNumber&quot;: 0,
    &quot;settlementDate&quot;: &quot;2020-09-21T17:47:59.703Z&quot;
  }
]

Update the code as well :

public class Sample {

	public static void main(String[] args) {
		String json = &quot;[{\n&quot; + 
				&quot;	\&quot;appId\&quot;: \&quot;MBSP\&quot;,\n&quot; + 
				&quot;	\&quot;askPrice\&quot;: 0,\n&quot; + 
				&quot;	\&quot;bidPrice\&quot;: 0,\n&quot; + 
				&quot;	\&quot;collectionDataSource\&quot;: \&quot;ExternalTick\&quot;,\n&quot; + 
				&quot;	\&quot;collectionName\&quot;: \&quot;FRM_MBS_TBA_FN_15Y_0.03_FWD0\&quot;,\n&quot; + 
				&quot;	\&quot;collectionObservationTime\&quot;: \&quot;2020-09-21T17:47:59.703Z\&quot;,\n&quot; + 
				&quot;	\&quot;collectionType\&quot;: \&quot;LIVE\&quot;,\n&quot; + 
				&quot;	\&quot;coupon\&quot;: 1.03,\n&quot; + 
				&quot;	\&quot;createdBy\&quot;: \&quot;Test\&quot;,\n&quot; + 
				&quot;	\&quot;createdOn\&quot;: \&quot;2020-09-21T17:47:59.703Z\&quot;,\n&quot; + 
				&quot;	\&quot;createdOnDate\&quot;: 0,\n&quot; + 
				&quot;	\&quot;forward\&quot;: 0,\n&quot; + 
				&quot;	\&quot;issuingAgency\&quot;: \&quot;FF\&quot;,\n&quot; + 
				&quot;	\&quot;lastUpdated\&quot;: \&quot;2020-09-21T17:47:59.703Z\&quot;,\n&quot; + 
				&quot;	\&quot;lastUpdatedBy\&quot;: \&quot;string\&quot;,\n&quot; + 
				&quot;	\&quot;lastUpdatedDate\&quot;: 0,\n&quot; + 
				&quot;	\&quot;maturity\&quot;: 0,\n&quot; + 
				&quot;	\&quot;midPrice\&quot;: 0,\n&quot; + 
				&quot;	\&quot;mtaVersionNumber\&quot;: 0,\n&quot; + 
				&quot;	\&quot;settlementDate\&quot;: \&quot;2020-09-21T17:47:59.703Z\&quot;\n&quot; + 
				&quot;}]&quot;;
		JSONArray jsonObj = new JSONArray(json);
		for (int i = 0; i &lt; jsonObj.length(); i++) {
			JSONObject jsonobject = jsonObj.getJSONObject(i);
			double Coupon = jsonobject.getDouble(&quot;coupon&quot;);
			System.out.println(Coupon);
		}
	}

}

Output :

1.03

答案2

得分: 1

如果您再次检查您的 JSON,您会注意到其中有一个数组嵌套在另一个数组中,然后是一个对象。
[ [ { } ] ]

尝试这个输入,[ { } ]

另一种处理方法是在代码中处理,以访问嵌套在 JSON 数组内部的 JSON 对象。

谢谢,希望对您有所帮助。

英文:

if you check again your json you will notice there is array in array and then object.
[ [ { } ] ]

Try this input, [ { } ]

[
{
&quot;appId&quot;: &quot;MBSP&quot;,
&quot;askPrice&quot;: 0,
&quot;bidPrice&quot;: 0,
&quot;collectionDataSource&quot;: &quot;ExternalTick&quot;,
&quot;collectionName&quot;: &quot;FRM_MBS_TBA_FN_15Y_0.03_FWD0&quot;,
&quot;collectionObservationTime&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
&quot;collectionType&quot;: &quot;LIVE&quot;,
&quot;coupon&quot;: 1.03,
&quot;createdBy&quot;: &quot;Test&quot;,
&quot;createdOn&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
&quot;createdOnDate&quot;: 0,
&quot;forward&quot;: 0,
&quot;issuingAgency&quot;: &quot;FF&quot;,
&quot;lastUpdated&quot;: &quot;2020-09-21T17:47:59.703Z&quot;,
&quot;lastUpdatedBy&quot;: &quot;string&quot;,
&quot;lastUpdatedDate&quot;: 0,
&quot;maturity&quot;: ,
&quot;midPrice&quot;:0 ,
&quot;mtaVersionNumber&quot;: 0,
&quot;settlementDate&quot;: &quot;2020-09-21T17:47:59.703Z&quot;
}
]

Another way is handle in code to access json array inside a
json array to get json object.

Thanks, I hope that helps you.

答案3

得分: 1

你的 JSON 输入似乎无效:

  • 没有 [[...]]
  • &quot;maturity&quot;: , 可能不是有效的 JSON 节点
  • 代码 String Coupon = jsonobject.getString(&quot;Coupon&quot;); 不正确

解决方法:

  • 更新 JSON 输入,如 [...]
  • maturity 更新为有效的偶数值,如果为空/为null
  • 将你的代码改为 String coupon = jsonobject.getString(&quot;coupon&quot;);
英文:

Your JSON input seems invalid:

  • No [[...]]
  • &quot;maturity&quot;: , maybe not valid json node
  • code String Coupon = jsonobject.getString(&quot;Coupon&quot;); not correct

Solution:

  • Update JSON input like. [...]
  • Update maturity to a valid even value is empty/null
  • Change you code to String coupon = jsonobject.getString(&quot;coupon&quot;);

huangapple
  • 本文由 发表于 2020年9月26日 12:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64073819.html
匿名

发表评论

匿名网友

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

确定