在java代码中解析json对象

在java代码中解析json对象,java,json,spring-mvc,Java,Json,Spring Mvc,我被代码困住了,实际上我使用的是SpringMVC4。我有一个条件,需要在控制器端传递json对象并对其进行迭代。我的Json对象如下所示 {"comptocome":[ {"1":"Key Parameters","2":"Cellular Limited","3":"limited","4":"Cellular Limited"}, {"1":"- Long term","2":"Reaffirmed","3":"football","4":"golf"} ] }

我被代码困住了,实际上我使用的是SpringMVC4。我有一个条件,需要在控制器端传递json对象并对其进行迭代。我的Json对象如下所示

{"comptocome":[
    {"1":"Key Parameters","2":"Cellular Limited","3":"limited","4":"Cellular Limited"},
    {"1":"- Long term","2":"Reaffirmed","3":"football","4":"golf"}
    ]
}
关于以上内容,我已经将其传递给控制器,并根据行数进行迭代,例如,从上面的两次循环中,以及根据键获取数据。任何人都可以帮助我在import org.json.simple.JSONObject包的帮助下解决此问题


提前感谢。

您可以使用com.google.gson:

@RequestMapping(value = "/request", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseClass addDeparturePoint(@RequestBody String request) {
    Gson gson = new Gson();
    RequestClass request = gson.fromJson(request, RequestClass.class);
    ResponseClass response = buildResponce(request);
    return response;

}使用Jackson JSON解析

例如:

可以映射到此类:

public class Fizzle{
    private List<String> foo;
    private boolean bar;
    private int baz;
    // getters and setters omitted
}
@RequestMapping("somepath")
@ResponseBody
public Fozzle doSomeThing(@RequestBody Fizzle input){
    return new Fozzle(input);
}

看看这个答案:你可以用Jackson来映射对象。可能的答案这里可能重复感谢大家的重播我已经通过,但没有找到任何适当的解决方案。请任何人在这里详细说明。实际上,我想使用org.json.simple.JSONArray、org.json.simple.JSONObject、org.json.simple.parser.JSONParser,但不是org.json one。谢谢你的回复,这真的是我认为没有我们可以实现的bean类的强迫吗。
@RequestMapping("somepath")
@ResponseBody
public Fozzle doSomeThing(@RequestBody Fizzle input){
    return new Fozzle(input);
}