org.json.JSONException:JSONArray [0]不是JSONObject-Java

时间:2020-09-26 04:14:19

标签: java arrays json

你好,我尝试读取和解析JSON文件,当我尝试读取它时,我发现= org.json.JSONException: JSONArray[0] is not a JSONObject异常。出于示例目的,JSON缩短了。提供的是我的代码,json和所需的输出。

代码:

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.getJSONObject(i);
            String Coupon = jsonobject.getString("Coupon");
            System.out.println(Coupon);
        }
    }

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"
  }
]]

想要的输出

 1.03

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

如果再次检查您的json,您会注意到先是array,然后是object。 [[{{}]]

尝试此输入[[}]

[ { "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" } ]

另一种方法是代码中的句柄以访问内部的json数组 json数组以获取json对象。

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

答案 2 :(得分:1)

您的JSON输入似乎无效:

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

解决方案:

  • 像这样更新JSON输入。 [...]
  • maturity更新为有效的偶数值为空/空
  • 将您的代码更改为String coupon = jsonobject.getString("coupon");