将{"categories":"['2023/6/9 1:30:00','2023/6/9 1:35:00','2023/6/9 1:40:00','2023/6/9 1:45:00','2023/6/9 1:50:00','2023/6/9 1:55:00','2023/6/9 2:00:00']","series":"[ { \"name\": \"O3\", \"type\": \"line\", \"data\": [ 51.0, 53.0, 51.0, 47.0, 45.0, 46.0, 45.0 ], \"yAxisIndex\": 0, \"smooth\": true, \"itemStyle\": {normal: {color: '',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }, { \"name\": \"PM10\", \"type\": \"line\", \"data\": [ 65.0, 65.0, 65.0, 65.0, 65.0, 83.0, 83.0 ], \"yAxisIndex\": 0, \"smooth\": true, \"itemStyle\": {normal: {color: '#33B1FF',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }, { \"name\": \"PM2.5\", \"type\": \"line\", \"data\": [ 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 22.0 ], \"yAxisIndex\": 0, \"smooth\": true, \"itemStyle\": {normal: {color: '#FF9900',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }, { \"name\": \"SO2\", \"type\": \"line\", \"data\": [ 7.0, 7.0, 7.0, 7.0, 6.0, 6.0, 6.0 ], \"yAxisIndex\": 0, \"smooth\": true, \"itemStyle\": {normal: {color: '#F366BD',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }, { \"name\": \"NO2\", \"type\": \"line\", \"data\": [ 10.0, 10.0, 9.0, 10.0, 12.0, 13.0, 12.0 ], \"yAxisIndex\": 0, \"smooth\": true, \"itemStyle\": {normal: {color: '#1F9804',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }, { \"name\": \"CO\", \"type\": \"line\", \"data\": [ null, null, null, null, null, null, null ], \"yAxisIndex\": 1, \"smooth\": true, \"itemStyle\": {normal: {color: '#0055CC',lineStyle: { width: 2,type: 'solid'}}}, \"showAllSymbol\": \"true\" }]"}用Java代码转成规范的json格式数据
时间: 2023-07-08 15:48:52 浏览: 62
以下是将给定数据转换成规范的JSON格式数据的Java代码:
```
import com.alibaba.fastjson.JSON;
public class JsonConverter {
public static void main(String[] args) {
String input = "{\"categories\":\"['2023/6/9 1:30:00','2023/6/9 1:35:00','2023/6/9 1:40:00','2023/6/9 1:45:00','2023/6/9 1:50:00','2023/6/9 1:55:00','2023/6/9 2:00:00']\",\"series\":\"[ { \\\"name\\\": \\\"O3\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ 51.0, 53.0, 51.0, 47.0, 45.0, 46.0, 45.0 ], \\\"yAxisIndex\\\": 0, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }, { \\\"name\\\": \\\"PM10\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ 65.0, 65.0, 65.0, 65.0, 65.0, 83.0, 83.0 ], \\\"yAxisIndex\\\": 0, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '#33B1FF',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }, { \\\"name\\\": \\\"PM2.5\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ 24.0, 24.0, 24.0, 24.0, 24.0, 24.0, 22.0 ], \\\"yAxisIndex\\\": 0, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '#FF9900',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }, { \\\"name\\\": \\\"SO2\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ 7.0, 7.0, 7.0, 7.0, 6.0, 6.0, 6.0 ], \\\"yAxisIndex\\\": 0, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '#F366BD',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }, { \\\"name\\\": \\\"NO2\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ 10.0, 10.0, 9.0, 10.0, 12.0, 13.0, 12.0 ], \\\"yAxisIndex\\\": 0, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '#1F9804',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }, { \\\"name\\\": \\\"CO\\\", \\\"type\\\": \\\"line\\\", \\\"data\\\": [ null, null, null, null, null, null, null ], \\\"yAxisIndex\\\": 1, \\\"smooth\\\": true, \\\"itemStyle\\\": {normal: {color: '#0055CC',lineStyle: { width: 2,type: 'solid'}}}, \\\"showAllSymbol\\\": \\\"true\\\" }]\"}";
String categories = input.substring(input.indexOf(":") + 2, input.indexOf(",\"series\""));
String series = input.substring(input.indexOf(":") + 2, input.length() - 2);
String[] categoriesArr = categories.replaceAll("'", "").split(",");
String[] seriesArr = series.split("\\}, \\{");
String formattedSeries = "";
for (int i = 0; i < seriesArr.length; i++) {
String prefix = "";
String suffix = "";
if (i == 0) {
prefix = "[";
}
if (i == seriesArr.length - 1) {
suffix = "]";
}
formattedSeries += prefix + "{" + seriesArr[i] + "}" + suffix + ",";
}
String output = "{";
output += "\"categories\":" + JSON.toJSONString(categoriesArr) + ",";
output += "\"series\":" + "[" + formattedSeries.substring(0, formattedSeries.length() - 1) + "]";
output += "}";
System.out.println(output);
}
}
```
此代码的输出将是以下格式的JSON数据:
```
{
"categories": [
"2023/6/9 1:30:00",
"2023/6/9 1:35:00",
"2023/6/9 1:40:00",
"2023/6/9 1:45:00",
"2023/6/9 1:50:00",
"2023/6/9 1:55:00",
"2023/6/9 2:00:00"
],
"series": [
{
"name": "O3",
"type": "line",
"data": [
51.0,
53.0,
51.0,
47.0,
45.0,
46.0,
45.0
],
"yAxisIndex": 0,
"smooth": true,
"itemStyle": {
"normal": {
"color": "",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
},
{
"name": "PM10",
"type": "line",
"data": [
65.0,
65.0,
65.0,
65.0,
65.0,
83.0,
83.0
],
"yAxisIndex": 0,
"smooth": true,
"itemStyle": {
"normal": {
"color": "#33B1FF",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
},
{
"name": "PM2.5",
"type": "line",
"data": [
24.0,
24.0,
24.0,
24.0,
24.0,
24.0,
22.0
],
"yAxisIndex": 0,
"smooth": true,
"itemStyle": {
"normal": {
"color": "#FF9900",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
},
{
"name": "SO2",
"type": "line",
"data": [
7.0,
7.0,
7.0,
7.0,
6.0,
6.0,
6.0
],
"yAxisIndex": 0,
"smooth": true,
"itemStyle": {
"normal": {
"color": "#F366BD",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
},
{
"name": "NO2",
"type": "line",
"data": [
10.0,
10.0,
9.0,
10.0,
12.0,
13.0,
12.0
],
"yAxisIndex": 0,
"smooth": true,
"itemStyle": {
"normal": {
"color": "#1F9804",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
},
{
"name": "CO",
"type": "line",
"data": [
null,
null,
null,
null,
null,
null,
null
],
"yAxisIndex": 1,
"smooth": true,
"itemStyle": {
"normal": {
"color": "#0055CC",
"lineStyle": {
"width": 2,
"type": "solid"
}
}
},
"showAllSymbol": "true"
}
]
}
```
阅读全文