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 = "[[{"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"
}
]]
Wanted ooutput
1.03
Any help would be appreciated.