简体   繁体   English

Java:将JSON结构反序列化为Map <String, Object>

[英]Java: Deserializing JSON structure to Map<String, Object>

I've got a JSON string that I want to convert to a Map structure where Object is either a Java version of a basic type (ie String, Int, Double), a Map. 我有一个要转换为Map结构的JSON字符串,其中Object是基本类型的Java版本(即String,Int,Double)或Map。 or a List. 或列表。

The sample string I'm using for my tests is: 我用于测试的示例字符串是:

"{\"cases\":[{\"documents\":[{\"files\":[{\"name\":\"a.pdf\"}]}]}]}"

This should read as an array of cases that each have an array of documents, that each have an array of files, that each have a name 这应该理解为一系列案例,每个案例都有一个文档数组,每个都有一个文件数组,每个都有一个名称

I've tried Google's Gson, but 我尝试过Google的Gson,但是

Gson gson = new Gson();
List<Map<String, Object>> results = gson.fromJson(dictString, List.class);

gives me: 给我:

com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@561777b1 failed to deserialize json object {"cases":[{"documents":[{"files":[{"name":"a.pdf"}]}]}]} given the type interface java.util.List

and I tried Jackson, but 我尝试了杰克逊,但是

List<Map<String, Object>> results = (List<Map<String, Object>>) new ObjectMapper().readValue(dictString, List.class);

gave me: 给我:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
 at [Source: java.io.StringReader@1c5aebd9; line: 1, column: 1]

Do you have any suggestions? 你有什么建议吗? Either for how to use either of the above correctly, or for another parser that gives me what I want? 如何正确使用以上两种方法之一,或者用于提供我想要的其他解析器?

Cheers 干杯

Nik

I stumbled in here with the same problem and I found a simple solution. 我在这里偶然遇到了同样的问题,并且找到了一个简单的解决方案。 I'm posting a more clear answer just in case it helps someone else: 我发布了一个更明确的答案,以防万一它可以帮助其他人:

String jsonString = "{ \"first-name\" : \"John\" }";

//creates, essentially a wrapper for a HashMap containing your JSON dictionary
JSONObject genericMap = new JSONObject(jsonString);

//calls to "put" and "get" are delegated to an internal hashmap
String name = (String) genericMap.get("first-name");
assertEquals("John", name); //passes

genericMap.put("last-name", "Doe"); //add any object to the hashmap

//the put methods can be used fluidly
genericMap.put("weight", 150).put("height", "5'10").put("age", "32");

//finally, you can write it back out to JSON, easily
String newJson = genericMap.toString(4); //pretty-print with 4 spaces per tab
log.debug(newJson);

this prints the following: 这将打印以下内容:

{
    "age": "32",
    "first-name": "John",
    "height": "5'10",
    "last-name": "Doe",
    "weight": 150
}

Add this dependency to your project like this: 将此依赖项添加到您的项目中,如下所示:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

Or download the JAR directly: 或直接下载JAR:
http://repo1.maven.org/maven2/org/json/json/20090211/json-20090211.jar http://repo1.maven.org/maven2/org/json/json/20090211/json-20090211.jar

I already had this class available (it was a transient dependency of something else in our project). 我已经有可用的此类(这是我们项目中其他内容的暂时依赖)。 So be sure to check if it's already there first. 因此,请务必先检查它是否已经存在。 You might get lucky, too. 您也可能会很幸运。

Given your description, it sounds like your definition doesn't match up. 根据您的描述,听起来您的定义不匹配。 It dounds like it should be something like: List<List<list<String>>> 它喜欢这样的东西: List<List<list<String>>>

It's a bit more manual but have a look here: 它有点手动,但是在这里看看:

http://json.org/java/ http://json.org/java/

This will give you a JSONObject that is much easier to use than parsing the string yourself, but you will still have to drill into it and manually build your map. 这将为您提供一个JSONObject,比您自己解析该字符串要容易得多,但是您仍然需要深入研究它并手动构建地图。 Kind of a half and half solution. 一种一半的解决方案。

最简单的事情可能就是自己做:使用GSON或挂毯的JSONObject之类的东西来构造json的Java表示,然后对其进行迭代并构建您喜欢的任何地图结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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