简体   繁体   中英

Parsing json file in java without using org.json

I'm trying to extract the first element in key and the value from the following json data. However, most examples I've seen have been using org.json which seems to be outdated? What would be the best way to do this with the following json file?

 "data": [
    {
      "key": [
        "01",
        "2015"
      ],
      "values": [
        "2231439"
      ]
    },
    {
      "key": [
        "03",
        "2015"
      ],
      "values": [
        "354164"
      ]
    },
    {
      "key": [
        "04",
        "2015"
      ],
      "values": [
        "283712"
      ]
    }
  ]
}

This is how I get the json response and store it in a String which gives the json data from above.

HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            OutputStream os = httpConnection.getOutputStream();
            os.write(jsonText.getBytes());
            os.flush();
            os.close();

            int responseCode = httpConnection.getResponseCode();
            System.out.println(responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
                String input;
                StringBuffer response = new StringBuffer();

                while ((input = br.readLine()) != null) {
                    response.append(input);
                }
                br.close();
                String responseJson = response.toString();

You could try Google's gson if you wanted an alternative? https://github.com/google/gson

Well I tried the below making use of Jackson API. Basically I have created a class which would be a Java representation of the entire JSON data

public class MyData {

    private List<Map<String, List<String>>> data;

    public List<Map<String, List<String>>> getData() {
        return data;
    }

    public void setData(List<Map<String, List<String>>> data) {
        this.data = data;
    }

}

Wrote the below parser making use of the Jackson API, however the "keys" as mentioned in the JSON you have described, would have list of values as String.

For eg both 01 and 2015 would be items in the List for that "key" .

Note that I have dumped your JSON data into a file and am reading the JSON from therein.

public static void main(String[] args) {

    ObjectMapper mapper = new ObjectMapper();
    try {

        MyData myData = mapper.readValue(new File("data"), MyData.class);

        for (Map<String, List<String>> map : myData.getData()) {

            // To retrieve the first element from the list //
            // map.get("key") would return a list 
            // in order to retrieve the first element
            // wrote the below

            System.out.println(map.get("key").get(0));
        }

    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Note: As you have retrieved the JSON in a string, kindly make use of the below code

MyData myData = mapper.readValue(responseJson, MyData.class);

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