简体   繁体   中英

How to Parse JSONArray of JSONObjects, and the JSONObjects with JSONArray Inside in JAVA?

[
   {
    "dataset": "Kushman",
    "iIndex": 1964,
    "sQuestion": "The grocer has peanuts for 3.75 dollars a pound and walnuts for 2.75 dollars a pound. How many pounds of peanuts and walnuts must we mix to get 40 pounds of mixture to sell for 3.00 dollars per pound. ",
    "lEquations": [
      "(3.75*peanuts)+(2.75*walnuts)=3.0*40.0",
      "peanuts+walnuts=40.0"
    ],
    "lSolutions": [
      10.0,
      30.0
    ],
    "grammarCheck": 1,
    "templateNumber": 4
  },
  {
    "dataset": "Kushman",
    "iIndex": 2003,
    "sQuestion": "Admission tickets to a football game were 60 cents for adults and 25 cents for children. Receipts for the day showed that 280 persons attended and 140 dollars was collected. How many adults attended? How many children attended?",
    "lEquations": [
      "(60*.01*noof_adults)+(25*.01*noof_childrens)=140.0",
      "noof_adults+noof_childrens=280.0"
    ],
    "lSolutions": [
      200.0,
      80.0
    ],
    "grammarCheck": 1,
    "templateNumber": 2
  }
]

This is the Json File named as "Kushman.json". I want to parse it and save the results in different text files for Dataset, Questions and Solution as in the JSON file.

Use Jackson,

ObjectMapper mapper = new ObjectMapper();
Dataset dataset = mapper.readValue(jsonString, Dataset.class);

Dataset create according to json structure

or

Quick-json Parser,

JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonMap=parser.parseJson(jsonString);

I prefer Gson but its up to you.

First create a model which gets mapped with your Json data.

public class MyModel {
    private String dataset;
    private int iIndex;
    private String sQuestion;
    private String[] lEuqations;   
    private float[] lSolutions;
    private int grammarCheck;
    private int templateNumber;
 }

After you can map your data with Gson.

 Gson gson = new GsonBuilder().create();
 List<MyModel> yourModel = gson.fromJson(jsonData, MyModel[].class)

Thats it.

Do not forget to add gson to your gradle (if using gradle).

// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'

Using JsonObjects and JsonArrays its possible.This is just an example how to read json array and object.Using this you can write values back to files.

JSONObject jObject = null;
    try {

    String res=FileUtils.readFileToString(new File("test.txt"));        
       jObject = new JSONObject(res);
       JSONArray j1 = jObject.JSONArray ("dataset");
       System.out.println(j1);
        j2 = j1.getJSONObject("lEquations");           
       System.out.println(j2);
    } catch (Exception e) {
        System.out.println("Exception: "+e.getMessage());

    }

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