我如何创建一个 JSON 对象列表,其中所有的键都是唯一的 GUID?

huangapple go评论108阅读模式
英文:

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:

{
&quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx1&quot;: {
&quot;values&quot;: {
 &quot;item1&quot;:&quot;value1&quot;
&quot;item2&quot;:&quot;value2&quot;
&quot;item3&quot;:&quot;value3&quot;
}
},
&quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx2&quot;: {
&quot;values&quot;: {
 &quot;item1&quot;:&quot;value1&quot;
&quot;item2&quot;:&quot;value2&quot;
&quot;item3&quot;:&quot;value3&quot;
}
},
......more entries
}

I was hoping to get something like the following with the guid called out, but no luck:

{
&quot;guid&quot;:&quot;xxxxxxxe-xxxx-xxxx-xxxx-xxxxxx1&quot;,
&quot;values&quot;: {
  &quot;item1&quot;:&quot;value1&quot;
  &quot;item2&quot;:&quot;value2&quot;
  &quot;item3&quot;:&quot;value3&quot;
 }
}

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&lt;String, Map&lt;String, Map&lt;String, String&gt;&gt;&gt; attributes = new HashMap&lt;String,Map&lt;String, Map&lt;String, String&gt;&gt;&gt;();
attributes =new Gson().fromJson(jsonString, Map.class);

//and then...
Map valueString = attributes.get(&quot;xxxx-xxx-xxx);
Map nestMap= (Map) valueString.get(&quot;values&quot;);
String theInfoINeed = (String)nestMap.get(&quot;item1&quot;);


答案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&lt;String, Map&lt;String, Map&lt;String, String&gt;&gt;&gt;. 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(&quot;./resource/test.json&quot;).getAbsoluteFile();

		ObjectMapper mapper = new ObjectMapper();
		Map&lt;String, Map&lt;String, Object&gt;&gt; result = mapper.readValue(jsonFile, new TypeReference&lt;Map&lt;String, Map&lt;String, Object&gt;&gt;&gt;() {});

		List&lt;Item&gt; items = result.entrySet().stream().map(e -&gt; {
			Item item = mapper.convertValue(e.getValue().get(&quot;values&quot;), 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)

huangapple
  • 本文由 发表于 2020年7月28日 02:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定