我的问题是如何从设备的api解析json响应。json如下所示。
[{“name”:“device 1”,“label”:“switch state”,“value”:“off”},{“name”:“device 2”,“label”:“switch state”,“value”:“on”}]
public class Fubar{
public static void main(String[] args) throws Exception {
boolean a = true;
while (a) {
Configuration conf = new Configuration();
FileSystem hdfs = FileSystem.get(conf);
String fileName = "filethingy" + "-" + new Date().getTime() + ".txt";
boolean b = true;
while(b){
String connString = "url";
URL url = new URL(connString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
out.writeBytes("TimeStamp"+",");
out.writeBytes("DeviceName"+",");
out.writeBytes("DeviceLabel"+",");
out.writeBytes("DeviceStatus"+",");
out.writeBytes("\n");
while ((inputLine = in.readLine()) != null) {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(inputLine);
String date = new java.sql.Timestamp(System.currentTimeMillis()).toString();
out.write(date.getBytes());
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("name"));
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("label"));
out.write(",".getBytes());
out.writeBytes((String) jsonObject.get("value"));
//out.write(inputLine.getBytes());
out.write("\n".getBytes());
out.hflush();
}
in.close();
Thread.sleep(30000);
out.hflush();
}
out.close();
hdfs.close();
a=false;
b=false;
}
我知道这行不通,因为我无法将jsonarray转换为jsonobject。我很难弄清楚如何正确地分离json来最终构建一个csv,它看起来像timestamp、devicename、devicelabel、devicestatus、01012001、device1、switch status等等。我们完成后,会有人在数据上放一个Hive表。任何帮助都将不胜感激。
1条答案
按热度按时间3qpi33ja1#
回答了我自己的问题。