{"dimName":"模型分值","echatsName":"私家车燃油车","map":{"x":["1","2","3","4","5","6","7","8","9","10"],"y":[{"name":"2024 - 求和项:CUR_REAL_ANP_ADJ","ratio":[0.215,0.127,0.1083,0.0989,0.0899,0.0819,0.0763,0.0715,0.0665,0.0607],"type":"1"},{"name":"2024 - 求和项:PLR","ratio":[0.4833,0.5622,0.6001,0.6325,0.6656,0.7,0.7401,0.7892,0.8639,1.0385],"type":"3"},{"name":"2024 - 求和项:LR","ratio":[0.4237,0.4333,0.6267,0.5871,0.5951,0.6223,0.837,0.8298,0.6898,0.8478],"type":"2"}],"issueMonth":["2024 - 求和项:CUR_REAL_ANP_ADJ","2024 - 求和项:PLR","2024 - 求和项:LR"]},"title":"提升效果","type":"2"} 把这种数据转换为这种格式怎么转换 { "dimName":"{dimName}", "title":"{title}", "x":"所提取的{x}数据" "data"{ “{y}中的{name}”:[{x}中所对应的{y}中的{radio}数据] } }
时间: 2025-07-01 12:55:23 浏览: 3
你提供的原始数据结构是一个嵌套的 JSON 对象,其中包含 `x` 坐标轴数据和 `y` 中多个带有 `name` 和 `ratio` 的数据项。你需要将其转换为如下格式:
```json
{
"dimName": "{dimName}",
"title": "{title}",
"x": "[所提取的x数据]",
"data": {
"{y}中的{name}": [对应 ratio 数据]
}
}
```
---
### ✅ 转换逻辑说明
1. **提取字段**:
- `dimName`:直接从原始对象中获取;
- `title`:直接从原始对象中获取;
- `x`:来自 `map.x`;
- `data`:将 `map.y` 中的每个对象的 `name` 作为键,其 `ratio` 数组作为值。
2. **目标结构示例**(简化):
```json
{
"dimName": "模型分值",
"title": "提升效果",
"x": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
"data": {
"2024 - 求和项:CUR_REAL_ANP_ADJ": [0.215, 0.127, ..., 0.0607],
"2024 - 求和项:PLR": [0.4833, 0.5622, ..., 1.0385],
"2024 - 求和项:LR": [0.4237, 0.4333, ..., 0.8478]
}
}
```
---
### ✅ Java代码实现(使用 FastJSON)
```java
import com.alibaba.fastjson.*;
import java.util.*;
public class JsonDataTransformer {
public static void main(String[] args) {
String jsonData = "{\"dimName\":\"模型分值\",\"echatsName\":\"私家车燃油车\",\"map\":{\"x\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\"],\"y\":[{\"name\":\"2024 - 求和项:CUR_REAL_ANP_ADJ\",\"ratio\":[0.215,0.127,0.1083,0.0989,0.0899,0.0819,0.0763,0.0715,0.0665,0.0607],\"type\":\"1\"},{\"name\":\"2024 - 求和项:PLR\",\"ratio\":[0.4833,0.5622,0.6001,0.6325,0.6656,0.7,0.7401,0.7892,0.8639,1.0385],\"type\":\"3\"},{\"name\":\"2024 - 求和项:LR\",\"ratio\":[0.4237,0.4333,0.6267,0.5871,0.5951,0.6223,0.837,0.8298,0.6898,0.8478],\"type\":\"2\"}],\"issueMonth\":[\"2024 - 求和项:CUR_REAL_ANP_ADJ\",\"2024 - 求和项:PLR\",\"2024 - 求和项:LR\"]},\"title\":\"提升效果\",\"type\":\"2\"}";
JSONObject original = JSON.parseObject(jsonData);
JSONObject map = original.getJSONObject("map");
// 提取 x 轴数据
List<String> xList = map.getJSONArray("x").toJavaList(String.class);
// 提取 y 数据
JSONArray yArray = map.getJSONArray("y");
Map<String, List<Double>> dataMap = new LinkedHashMap<>();
for (Object obj : yArray) {
JSONObject item = (JSONObject) obj;
String name = item.getString("name");
List<Double> ratio = item.getJSONArray("ratio").toJavaList(Double.class);
dataMap.put(name, ratio);
}
// 构建结果
JSONObject result = new JSONObject();
result.put("dimName", original.getString("dimName"));
result.put("title", original.getString("title"));
result.put("x", xList);
JSONObject dataObj = new JSONObject();
for (Map.Entry<String, List<Double>> entry : dataMap.entrySet()) {
dataObj.put(entry.getKey(), entry.getValue());
}
result.put("data", dataObj);
System.out.println(result.toJSONString());
}
}
```
---
### ✅ 依赖要求
同样使用阿里巴巴的 `FastJSON`,请确保引入以下 Maven 依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
```
---
### ✅ 输出结果示例
```json
{
"dimName": "模型分值",
"title": "提升效果",
"x": ["1","2","3","4","5","6","7","8","9","10"],
"data": {
"2024 - 求和项:CUR_REAL_ANP_ADJ": [0.215, 0.127, 0.1083, 0.0989, 0.0899, 0.0819, 0.0763, 0.0715, 0.0665, 0.0607],
"2024 - 求和项:PLR": [0.4833, 0.5622, 0.6001, 0.6325, 0.6656, 0.7, 0.7401, 0.7892, 0.8639, 1.0385],
"2024 - 求和项:LR": [0.4237, 0.4333, 0.6267, 0.5871, 0.5951, 0.6223, 0.837, 0.8298, 0.6898, 0.8478]
}
}
```
---
###
阅读全文
相关推荐

















