gpt4 book ai didi

java - 使用 java 中的键对 Json 响应进行分组 - android studio

转载 作者:行者123 更新时间:2023-12-01 16:51:28 24 4
gpt4 key购买 nike

我是java和android开发的新手。我正在学习json解析。我收到类似这样的 json 响应。

{"code":"0","data":    
[ {"chrDesigName":"Developer","chrempName":"Test Employee1"},
{"chrDesigName":"Developer","chrempName":"Test Employee2"},
{"chrDesigName":"Tester","chrempName":"Test Employee3"},
{"chrDesigName":"Analyst","chrempName":"Test Employee4"},
{"chrDesigName":"Developer","chrempName":"Test Employee5"},
{"chrDesigName":"Tester","chrempName":"Test Employee6"}]

我想把这个 Json 解析成这样的东西。

{"Developer" : [{"chrempName" : "Test Employee1"},
{"chrempName" : "Test Employee2"},
{"chrempName" : "Test Employee5"}],
"Tester" : [{"chrempName" : "Test Employee3"},
{"chrempName" : "Test Employee6"}],
"Analyst" : [{"chrempName" : "Test Employee4"}]}

我怎样才能实现这个目标?我的动机是根据所有员工的职责对他们进行分组。

最佳答案

作为一个快速技巧,我使用了 2 个不同的库来做到这一点。您也可以使用 jackson “统一”。

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class JsonReformatter {

public static void main(String[] args) throws Exception {
String jsonString = "{\"code\": \"0\",\"data\": [{\"chrDesigName\": \"Developer\",\"chrempName\": \"Test Employee1\"},{\"chrDesigName\": \"Developer\",\"chrempName\": \"Test Employee2\"},{\"chrDesigName\": \"Tester\",\"chrempName\": \"Test Employee3\"},{\"chrDesigName\": \"Analyst\",\"chrempName\": \"Test Employee4\"},{\"chrDesigName\": \"Developer\",\"chrempName\": \"Test Employee5\"},{\"chrDesigName\": \"Tester\",\"chrempName\": \"Test Employee6\"}]}";

ObjectMapper mapper = new ObjectMapper();

// Map<String, Object> dataMap = mapper.readValue(jsonString,
// new TypeReference<Map<String, Object>>() {
// });
//
// System.out.println(dataMap.get("data") +
// ((ArrayList)dataMap.get("data")).get(0).getClass().getName()) ;

JsonArray dataArray = new JsonParser().parse(jsonString).getAsJsonObject().getAsJsonArray("data");

HashMap<String, List<String>> designationsMap = new HashMap<String, List<String>>();

for (JsonElement element : dataArray) {
String designation = element.getAsJsonObject().get("chrDesigName").getAsString();
String empName = element.getAsJsonObject().get("chrempName").getAsString();
if (designationsMap.containsKey(designation)) {
designationsMap.get(designation).add(empName);
} else {
ArrayList<String> emptyList = new ArrayList<String>();
emptyList.add(empName);
designationsMap.put(designation, emptyList);
}
}

StringWriter result = new StringWriter();
mapper.writeValue(result, designationsMap);
System.out.println(result);

}

}

这会打印

{"分析师":["测试员工4"],"测试员":["测试员工3","测试 员工6"],"开发人员":["测试员工1","测试员工2","测试员工5"]}

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>

关于java - 使用 java 中的键对 Json 响应进行分组 - android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com