简体   繁体   中英

need to read the json arrays inside the object using java

I am trying to read the JSON using java but unable to do that. So need to write a java code read the JSON file where arrays are inside the object.

"exclusion":{  
     "serviceLevelList":[ "SIS98", "C4P","SNTP" ],
     "pid":[  "ABC", "DEF"  ]
}

Use JSONObject for simple JSON and JSONArray for array of JSON.

try {
                JSONParser parser = new JSONParser();

                JSONObject data = (JSONObject) parser.parse(
                      new FileReader("/config.json"));//path to the JSON file.

                JSONObject jsonObject = data.getJSONObject("exclusion");
                JSONArray array= jsonObject.getJSONArray("pid");


            } catch (Exception e) {
                e.printStackTrace();
            }

Use google-simple library

<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>

Try this:

String jsonTxt = IOUtils.toString( is );
    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt ); 

     JSONObject exclusion= json.getJSONObject("exclusion");
     String serviceLevelList[]= pilot.getString("serviceLevelList");
     String pid[]= pilot.getString("pid");

You Can Try methods Of Gson Object to convert JSON to Java Object and Vise Versa.

for That you Can Use Dependancy as follows

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>

Gson object Provides several methods as follows :

    Gson gson = new Gson();

// Convert Java object to JSON and assign to a String
    String jsonInString = gson.toJson(obj);

//Convert JSON to Java object, read it from a JSON String.
    String jsonInString = "{'name' : 'myname'}";
    Staff staff = gson.fromJson(jsonInString, Student.class);

you can try this with your code :-)

We have been using XStream for years now. Although our main use has been for .XML files, it also supports reading and writing JSON as well, and we've used like that for a couple of times.

Include it in your maven project with this dependency snippet:

  <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <version>1.4.11</version>
  </dependency>

You have all info you need in their web site . They even have a " Two minute tutorial " and a " JSON Tutorial " that might be of use (which, by the way, has a " Read from JSON " mention that might be directly applicable to your case). There are also several posts across the internet, as they documented in their references section , and even a XStream course in StudyTrails .

By using JSONObject and JSONArray classes you can perform different operation on json data.
Refer this link for know about handling the json data of different format,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM