{ "type": "NORMAL", "var": { "id": 2808147, "name": "A相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC000", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ua", "record_period": "5分钟", "rw": 1 }, "name": "A相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.079, 6.079, 6.079 ] },{ "type": "NORMAL", "var": { "id": 2808148, "name": "B相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC001", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ub", "record_period": "5分钟", "rw": 1 }, "name": "B相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.064, 6.074, 6.065 ] } 以上为返回的数据格式,如何通过Java代码处理成 ,时间 A相电压 B相电压这种表格形式
时间: 2023-06-28 12:13:54 浏览: 118
您可以使用Java中的JSONObject和JSONArray类来解析这个JSON字符串,然后将数据按照表格形式输出。
下面是一个示例代码:
```
import org.json.*;
public class JsonToTable {
public static void main(String[] args) throws JSONException {
// 假设jsonStr为接口返回的JSON字符串
String jsonStr = "[{\"type\":\"NORMAL\",\"var\":{\"id\":2808147,\"name\":\"A相电压\",\"unit\":\"kV\",\"type\":\"Analog\",\"sn\":\"Ahhy_CPZX_G101__YC000\",\"device_sn\":\"Ahhy_CPZX_G101\",\"device_name\":\"进线\",\"parent_index\":\"Ahhy\",\"zero_meaning\":null,\"one_meaning\":null,\"var_code\":\"Ua\",\"record_period\":\"5分钟\",\"rw\":1},\"name\":\"A相电压\",\"unit\":\"kV\",\"time_keys\":[\"2023-05-25 00:00:00.000\",\"2023-05-25 00:05:00.000\",\"2023-05-25 00:10:00.000\",\"2023-05-25 00:15:00.000\",\"2023-05-25 00:20:00.000\"],\"datas\":[6.079,6.079,6.079,6.079,6.079]},{\"type\":\"NORMAL\",\"var\":{\"id\":2808148,\"name\":\"B相电压\",\"unit\":\"kV\",\"type\":\"Analog\",\"sn\":\"Ahhy_CPZX_G101__YC001\",\"device_sn\":\"Ahhy_CPZX_G101\",\"device_name\":\"进线\",\"parent_index\":\"Ahhy\",\"zero_meaning\":null,\"one_meaning\":null,\"var_code\":\"Ub\",\"record_period\":\"5分钟\",\"rw\":1},\"name\":\"B相电压\",\"unit\":\"kV\",\"time_keys\":[\"2023-05-25 00:00:00.000\",\"2023-05-25 00:05:00.000\",\"2023-05-25 00:10:00.000\",\"2023-05-25 00:15:00.000\",\"2023-05-25 00:20:00.000\"],\"datas\":[6.079,6.079,6.064,6.074,6.065]}]";
JSONArray jsonArray = new JSONArray(jsonStr);
// 获取表头
String[] header = {"时间", "A相电压", "B相电压"};
// 构建二维数组,存储表格数据
int rows = jsonArray.getJSONObject(0).getJSONArray("time_keys").length();
double[][] data = new double[rows][header.length - 1];
// 解析JSON数据,将数值存入data数组
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
JSONArray datas = jsonObject.getJSONArray("datas");
for (int j = 0; j < datas.length(); j++) {
data[j][i] = datas.getDouble(j);
}
}
// 输出表格
System.out.print(String.format("%-20s", header[0]));
for (int i = 1; i < header.length; i++) {
System.out.print(String.format("%-20s", header[i]));
}
System.out.println();
for (int i = 0; i < rows; i++) {
System.out.print(String.format("%-20s", jsonArray.getJSONObject(0).getJSONArray("time_keys").getString(i)));
for (int j = 0; j < data[i].length; j++) {
System.out.print(String.format("%-20s", data[i][j]));
}
System.out.println();
}
}
}
```
输出结果:
```
时间 A相电压 B相电压
2023-05-25 00:00:00.0006.079 6.079
2023-05-25 00:05:00.0006.079 6.079
2023-05-25 00:10:00.0006.079 6.064
2023-05-25 00:15:00.0006.079 6.074
2023-05-25 00:20:00.0006.079 6.065
```
阅读全文