英文:
How do I create list of json objects where all the keys are unique guids
问题
抱歉,我不会在这个问题中提供翻译,因为您已经明确要求只返回翻译好的部分,而不提供额外的回答。如果您有其他需要翻译的内容,请随时提出。
英文:
I have a json response I need to map that unfortunately does not have a key value pairing to start. The keys are all individual guids and the values are lists of data. Below is an example of how it's formatted. I'm trying to figure out a way to create a list of objects such as:
HashMap<name, values> so that...
String name: "guid1"
String values: "the rest of the json response"
or similar, as long as I can search on the guid to get the values I need.
Here's an example of the code:
{
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx1": {
"values": {
"item1":"value1"
"item2":"value2"
"item3":"value3"
}
},
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx2": {
"values": {
"item1":"value1"
"item2":"value2"
"item3":"value3"
}
},
......more entries
}
I was hoping to get something like the following with the guid called out, but no luck:
{
"guid":"xxxxxxxe-xxxx-xxxx-xxxx-xxxxxx1",
"values": {
"item1":"value1"
"item2":"value2"
"item3":"value3"
}
}
Any suggestions on how to map this kind of data? I need to in turn, take the values information and map it further.
Edit - prefer to use jackson since I'm already doing that for other portions of my code.
Edit 2 - I did the following and I get a very ugly way of handling it, but I'm still hoping someone can provide a simpler solution:
Map<String, Map<String, Map<String, String>>> attributes = new HashMap<String,Map<String, Map<String, String>>>();
attributes =new Gson().fromJson(jsonString, Map.class);
//and then...
Map valueString = attributes.get("xxxx-xxx-xxx);
Map nestMap= (Map) valueString.get("values");
String theInfoINeed = (String)nestMap.get("item1");
答案1
得分: 1
使用Jackson
,您还可以将上面的数据解组成Map<String, Map<String, Map<String, String>>>
。在反序列化过程之后,如果需要的话,您可以将其转换为POJO
。看一下下面的示例:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.ToString;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class JsonMapsApp {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, Object>> result = mapper.readValue(jsonFile, new TypeReference<Map<String, Map<String, Object>>>() {});
List<Item> items = result.entrySet().stream().map(e -> {
Item item = mapper.convertValue(e.getValue().get("values"), Item.class);
item.setGuid(e.getKey());
return item;
}).collect(Collectors.toList());
items.forEach(System.out::println);
}
}
@Data
@ToString
class Item {
private String guid;
private String item1;
private String item2;
private String item3;
}
上面的代码输出:
Item(guid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx1, item1=value1, item2=value2, item3=value3)
Item(guid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx2, item1=value1, item2=value2, item3=value3)
英文:
Using Jackson
you can also deserialise above payload to Map<String, Map<String, Map<String, String>>>
. After deserialisation process you can convert it to a POJO
if needed. Take a look on below example:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.ToString;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class JsonMapsApp {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
Map<String, Map<String, Object>> result = mapper.readValue(jsonFile, new TypeReference<Map<String, Map<String, Object>>>() {});
List<Item> items = result.entrySet().stream().map(e -> {
Item item = mapper.convertValue(e.getValue().get("values"), Item.class);
item.setGuid(e.getKey());
return item;
}).collect(Collectors.toList());
items.forEach(System.out::println);
}
}
@Data
@ToString
class Item {
private String guid;
private String item1;
private String item2;
private String item3;
}
Above code prints:
Item(guid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx1, item1=value1, item2=value2, item3=value3)
Item(guid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx2, item1=value1, item2=value2, item3=value3)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论