0

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
      }
    }
  ]
}  
Praveen
  • 300
  • 5
  • 15
  • My question is, how you catch the element if it is in array of array. This is not duplicate of above link. – Praveen Feb 04 '19 at 12:22

2 Answers2

0

Try replacing this line

JSONArray req= (JSONArray) jsonObject.get("interactions");

With :

JSONArray req= (JSONArray)jsonObject.getJSONArray("interactions");
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

You can use following class as a reference to get your desired value.

Please make sure to use getJSONArray() and getJSONObject() method appropriately according to your output.

package com.test.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class Test {

    public static void main(String[] args) throws IOException, JSONException {
        String filePath = "C://Users//hello//Desktop//New Text Document.txt";
        String string = FileUtils.readFileToString(new File(filePath));
        JSONObject jsonObject = new JSONObject(string);
        JSONArray jsonArray = jsonObject.getJSONArray("interactions");
         for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject reqObj = jsonArray.getJSONObject(i);
             System.out.println("description: " + reqObj.get("description") +"\n");
             System.out.println("request body: " + reqObj.get("request")); // prints full request body 
             System.out.println("reponse body: " + reqObj.get("response") +"\n"); // Success
             JSONObject requestBoday = reqObj.getJSONObject("request");
             JSONObject body = requestBoday.getJSONObject("body");

         }
    }
}
Ravi Sapariya
  • 395
  • 2
  • 11