0

I have JSON File as below:

{
  "KerParameters": [
      {
        "cut": false,
        "velocity": 0,
        "sigma": 150,
        "Tau": 250,
        "deltaT": 30,
        "deltaX": 100,
        "minValue": 0.000001
      },
      {
        "cut": false,
        "velocity": 0,
        "sigma": 150,
        "Tau": 250,
        "deltaX": 100,
        "minValue": 0.000001
      }    
  ],
  "time": false,
  "lower": 1E-14,
  "qualit": 1.0,
  "vfth": 55.0,
  "vjt": 30.0,
  "lambf": 0.1,
  "lambs": 0.6,
  "lambdaj": 0.5,
  "qRpic": 0.5,
  "minV": 3.0,
  "minKernelSizeT": 0
}

Could someone please let me know how do I get array of KerParameters ? and then its subParameters (lamb,lambs etc.) using simple JSON parser ? I can already access other simple parameters like time,lower and all.

Well, I tried it like solution here How do I make a JSON object with multiple arrays?

JSONParser parser = new JSONParser();
        JSONObject data = (JSONObject) parser.parse(
                new FileReader("src/test/resources/testcase4/KernelParameters.json"));
data.KerParameters['K1'].cut // This dos not work as it does not identify KerParameters
1
  • You're not new here, and so I'm a bit surprised that you're asking this without showing more of what you yourself have tried as a solution. Where is your own code attempt? How is it not working? If you have none, then the question is too broad. Commented Sep 22, 2017 at 12:59

1 Answer 1

2

Ok So finally, I did it like this. Just in case it helps someone else:

JSONParser parser = new JSONParser();
        FileReader fr = new FileReader("path to json file");
        JSONObject data = (JSONObject) parser.parse(fr);
        JSONArray kernelParams = (JSONArray) data.get("KernelParameters");
for(int i=0; i<kernelParams.size();i++) {
            JSONObject jsonObject = (JSONObject) kernelParams.get(i);
            kparams.add(jsonObject);
        }
JSONObject jo = (JSONObject) kparams.get(0);
        System.out.println("to get Sigma for e.g.: " + jo.get("sigma"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.