I am parsing a json file using json parser object. I am not able to access the request structure and it's inner body content. I've written code like:
private static final String filePath = "D:\\score_api.json";
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
System.out.println("++++++++++++++++++\n"+jsonObject);
// prints the whole json content. success!
JSONObject structure = (JSONObject) jsonObject.get("provider");
System.out.println("provider name: " + structure.get("name"));
// prints the provider name. success!
JSONArray req= (JSONArray) jsonObject.get("interactions");
Iterator i = req.iterator();
while (i.hasNext()) {
JSONObject reqObj = (JSONObject) i.next();
System.out.println("description: " + reqObj.get("description") +"\n");
System.out.println("request body: " + reqObj.get("request")); // prints full request body
System.out.println("path: " + reqObj.get("path") +"\n"); // Failing, getting null value.
System.out.println("reponse body: " + reqObj.get("response") +"\n"); // Success
}
}
And it's output:
++++++++++++++++++
{"full json file content prints"}
provider name: SIS
description: API POST Score
request body: {"full request body prints"}
path: null
reponse body: {"status":200}
I am struggling to access request body content. And its child part. I want to access the value of 'path' and other inner values like 'adptPolVal', 'eEcoId' and Source structure.
I am a newbie to java, m trying but failing. Any help would appreciated ! Thanks in advance !
Here's my json file content...
{
"consumer": {
"name": "Consumer1"
},
"provider": {
"name": "provider_a"
},
"interactions": [
{
"description": "API Score",
"providerStates": [
{
"name": "",
"params": {}
}
],
"request": {
"method": "post",
"path": "Z123MI6/services/score",
"headers": {
"content-type": "application/json"
},
"body": {
"adptPolVal": true,
"datapoints": [
{
"dataId": " data.point.id ",
"source": {
"srcType": "sourceType.dev.admin",
"snum": "12345",
"instId": "intance id",
"contId": "container id",
"appId": "com.consumer."
},
"userId": "userId",
"ts": 1234567891011,
"lt": 11.12345,
"lng": 123.456,
"ipId": "192.168.1.1",
"geoGraph": ""
}
],
"eEcoId": "ecoId"
}
},
"response": {
"status": 200
}
}
]
}