如何从Java中获取CURL HTTP Request Json结果的答案

时间:2016-09-16 05:27:58

标签: java json curl httprequest

我从CURL JSON获得结果。但我不知道如何从结果中获取特定数据。

我想从此结果中过滤第一个数据 time_Stamp 不是时间格式。

请帮我查询/过滤数据和时间格式。

我的计划如下:

package IOT;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;    

public class NetClientPost {

    // http://localhost:8080/RESTfulExample/json/product/post
    public static void main(String[] args) {

      try {

        URL url = new URL("https://api.datonis.io/api/v2/datonis_query/sensor_event_raw_data");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("X-Auth-Token", "PnR9l96XZnrL3xyoYxuM9g");

        String input = "{\"sensor_key\":\"6f2159f998\",\"from\":\"2016/08/29 16:55:00\",\"to\":\"2016/08/29 17:05:00\",\"time_zone\":\"Mumbai\",\"per\":20,\"metrics\":\"1st Data\"}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        //if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            //throw new RuntimeException("Failed : HTTP error code : "
                //+ conn.getResponseCode());
        //}

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            //System.out.println(output);    

            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JsonParser jp = new JsonParser();
            JsonElement je = jp.parse(output);
            String prettyJsonString = gson.toJson(je);          

            System.out.println("Connection Status2: "+ prettyJsonString);    
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();    
     }    
    }    
}

//package com.objectplanet.chart.testing; 

此外,我在此处发布了我的结果,如下所示: 服务器输出....

Connection Status2: {
  "6f2159f998": {
    "total_event_count": 2666,
    "page_number": "1",
    "event_data": [
      {
        "time_stamp": 1472483225,
        "data": {
          "1st data": 20
        }
      },
      {
        "time_stamp": 1472483220,
        "data": {
          "1st data": 20
        }
      },
      {
        "time_stamp": 1472483215,
        "data": {
          "1st data": 20
        }
      },
      {
        "time_stamp": 1472483210,
        "data": {
          "1st data": 20
        }
      },

我想从此结果中过滤第一个数据 time_Stamp 的格式不正确

0 个答案:

没有答案