简体   繁体   English

如何在JAVA中解析JSONObjects的JSONArray以及带有JSONArray的JSONObjects?

[英]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". 这是名为“ Kushman.json”的Json文件。 I want to parse it and save the results in different text files for Dataset, Questions and Solution as in the JSON file. 我想解析它,然后将结果保存在JSON文件中的数据集,问题和解决方案的不同文本文件中。

Use Jackson, 使用杰克逊,

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

Dataset create according to json structure 数据集根据json结构创建

or 要么

Quick-json Parser, Quick-json解析器,

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

I prefer Gson but its up to you. 我更喜欢Gson,但取决于您。

First create a model which gets mapped with your Json data. 首先创建一个与您的Json数据映射的模型。

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 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). 不要忘记将gson添加到gradle中(如果使用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. 使用JsonObjects和JsonArrays是可能的,这只是一个读取json数组和对象的示例,使用它您可以将值写回到文件中。

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());

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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