简体   繁体   English

JSON请求到Java数据结构

[英]JSON request to Java data structure

I have following JSON request: 我有以下JSON请求:

{
    "one":{
        "key":"value",
        "key":"value",
        "key":"value"
    },
    "two":[
        "value"
    ],
    "three":[
        "value"
    ],
    "four":[
        "value"
    ]
}

How can I represent it as a Java object/data structure? 如何将其表示为Java对象/数据结构? I can normally take JSON starting from "two" and handle it as follows: 我通常可以从"two"开始使用JSON并按以下方式进行处理:

@RequestBody Map<String, List<String>> inputParams

any suggestions? 有什么建议么?

Try this... 尝试这个...

 Map<String, Object> inputParams;

 InnerObj innerObj = (InnerObj)inputParams.get("one");
 List<String> secondList = (List)inputParams.get("two");
 List<String> thirdLisr = (List)inputParams.get("three");
 '''

where 哪里

class InnerObj {
    String key1;
    String key2;
}

You can use Google GSON https://github.com/google/gson and parse your json to POJO and than use it. 您可以使用Google GSON https://github.com/google/gson并将json解析为POJO,然后再使用它。 For example: 例如:

{ "key1":"value1", "key2":"value2", "key3":"value3" }

Gson gson = new Gson();
MyObj myObj = gson.fromJson(jsonObj, MyObj.class);

Where MyObj.java: 其中MyObj.java:

public class MyObj {
    String key1;
    String key2;
    String key3;
}

And in your controller you can use @RequestParam(value = "jsonObjFromClient") String jsonObj annotation. 在您的控制器中,您可以使用@RequestParam(value = "jsonObjFromClient") String jsonObj批注。

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

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