Java 使用简单json解析设备数据,并在hdfs中构建逗号分隔的文件
我的问题是如何解析来自设备api的json响应。json如下所示 [{“名称”:“设备1”,“标签”:“开关” 状态“,”值“:”关闭“},{”名称“:”设备2“,”标签“:”开关 状态“,”值“:“on”}]Java 使用简单json解析设备数据,并在hdfs中构建逗号分隔的文件,java,json,hadoop,Java,Json,Hadoop,我的问题是如何解析来自设备api的json响应。json如下所示 [{“名称”:“设备1”,“标签”:“开关” 状态“,”值“:”关闭“},{”名称“:”设备2“,”标签“:”开关 状态“,”值“:“on”}] 我知道这不起作用,因为我无法将JSONArray转换为JSONObject。我很难弄清楚如何正确地分离JSON来最终构建一个csv,它看起来像TimeStamp、DeviceName、DeviceLabel、DeviceStatus、01012001、Device1、Switch Sta
我知道这不起作用,因为我无法将JSONArray转换为JSONObject。我很难弄清楚如何正确地分离JSON来最终构建一个csv,它看起来像TimeStamp、DeviceName、DeviceLabel、DeviceStatus、01012001、Device1、Switch Status等等。当我们完成后,有人会在数据上放一个蜂巢表。任何帮助都将不胜感激。回答了我自己的问题
while ((inputLine = in.readLine()) != null) {
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(inputLine);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
String date = new java.sql.Timestamp(System.currentTimeMillis()).toString();
out.write(date.getBytes());
out.write(",".getBytes());
out.writeBytes((String) object.get("name"));
out.write(",".getBytes());
out.writeBytes((String) object.get("label"));
out.write(",".getBytes());
out.writeBytes(String.valueOf(object.get("value")));
//out.write(inputLine.getBytes());
out.write("\n".getBytes());
out.hflush();
}
while((inputLine=in.readLine())!=null){
JSONParser JSONParser=新的JSONParser();
JSONArray JSONArray=(JSONArray)jsonParser.parse(inputLine);
for(int i=0;i
while ((inputLine = in.readLine()) != null) {
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(inputLine);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
String date = new java.sql.Timestamp(System.currentTimeMillis()).toString();
out.write(date.getBytes());
out.write(",".getBytes());
out.writeBytes((String) object.get("name"));
out.write(",".getBytes());
out.writeBytes((String) object.get("label"));
out.write(",".getBytes());
out.writeBytes(String.valueOf(object.get("value")));
//out.write(inputLine.getBytes());
out.write("\n".getBytes());
out.hflush();
}