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
Ragini
  • 1,509
  • 7
  • 28
  • 42
  • 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. – Hovercraft Full Of Eels Sep 22 '17 at 12:59

1 Answers1

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"));
Ragini
  • 1,509
  • 7
  • 28
  • 42