Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to gather data from a JSON URL and display it
    primarykey
    data
    text
    <p>I am trying to write an automated Java test where the code will go to a specified URL, read the JSON data and print it up. Here is the JSON I am trying to access;</p> <pre><code>{ "status": "success", "records": [ { "timestamp": 1381222871868, "deviceId": "288", "temperature": 17 }, { "timestamp": 1381222901868, "deviceId": "288", "temperature": 17 }, { "timestamp": 1381222931868, "deviceId": "288", "temperature": 17 }, ]} </code></pre> <p>As you can see I only have 3 elements, Timestamp, DeviceId and Temperature.</p> <p>What I am ultimately aiming for it to be able to get 2 Timestamp values and take one value away from the other, if that is possible.</p> <p>Anyway I have been trying to do this all day and am having no luck whatsoever. I was recommended to use Gson and I have included the jar files into my classpath.</p> <p>If anyone knows anything or can help me in any way it would be much appreciated as I have exhausted Google and myself trying to work this out.</p> <p>Here is the code I have to display the full list, but I do not fully understand it and so far can't manipulate it to my advantage;</p> <pre><code>public static void main(String[] args) throws Exception { String jsonString = callURL("http://localhost:8000/eem/api/v1/metrics/temperature/288"); System.out.println("\n\njsonString: " + jsonString); // Replace this try catch block for all below subsequent examples /*try { JSONArray jsonArray = new JSONArray(jsonString); System.out.println("\n\njsonArray: " + jsonArray); } catch (JSONException e) { e.printStackTrace(); }*/ try { JSONArray jsonArray = new JSONArray(jsonString); int count = jsonArray.length(); // get totalCount of all jsonObjects for(int i=0 ; i&lt; count; i++) { // iterate through jsonArray JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position System.out.println("jsonObject " + i + ": " + jsonObject); } } catch (JSONException e) { e.printStackTrace(); } } public static String callURL(String myURL) { //System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) { urlConn.setReadTimeout(60 * 1000); } if (urlConn != null &amp;&amp; urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (Exception e) { throw new RuntimeException("Exception while calling URL:"+ myURL, e); } return sb.toString(); } </code></pre> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload