0

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();
kemkoi
  • 53
  • 1
  • 2
  • 9
  • Wat do you mean outdated?? – Nyakiba Oct 18 '16 at 11:51
  • @Nyakiba Its correct, org.json may not be the best there is. There are several others JSON Apis which have been recommended like Gson, Jackson, Genson and FlexJson. Checkout the discussion in the comment section for this link http://stackoverflow.com/a/18998203/3838328 – Kamal Kunjapur Oct 18 '16 at 14:54
  • Seems I've been in the dark – Nyakiba Oct 18 '16 at 15:05

2 Answers2

0

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

imrichardcole
  • 4,633
  • 3
  • 23
  • 45
0

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 e.g 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);
Kamal Kunjapur
  • 8,547
  • 2
  • 22
  • 32