简体   繁体   中英

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");


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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM