I have JSON`` data I'm getting with java GET request.
I need to count how many age objects are greater than 50 in the JSON object.
Right now I am just getting the whole JSON data line by line using bufferreader, but how do I get the single element age in the JSON object and compare it with the number 50?
package problem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String args[])
{
BufferedReader rd;
OutputStreamWriter wr;
try
{
URL url = new URL("https://coderbyte.com/api/challenges/json/age-counting");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
// Get the response
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {
System.out.println(e.toString());
}
}
Sample response for JSON data, I need to get the age value as an integer:
{
"data":
"key=IAfpK,
age=58,
key=WNVdi,
age=64,
key=jp9zt,
age=47"
}