2

I have following JSON request:

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

How can I represent it as a Java object/data structure? I can normally take JSON starting from "two" and handle it as follows:

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

any suggestions?

alalambda
  • 323
  • 3
  • 15

2 Answers2

0

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;
}
Ravindra Devadiga
  • 692
  • 1
  • 6
  • 14
  • Thanks, this seems legit and appropriate for me as I don't want to use a lot of external libs. – alalambda Aug 15 '16 at 12:10
  • but there's one issue: i'm getting java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to SomeObject. – alalambda Aug 15 '16 at 12:37
0

You can use Google GSON https://github.com/google/gson and parse your json to POJO and than use it. For example:

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

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

Where MyObj.java:

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

And in your controller you can use @RequestParam(value = "jsonObjFromClient") String jsonObj annotation.