我有以下请求映射:
@RequestMapping(value = "/reCalculated", method = RequestMethod.POST)
public @ResponseBody void reCalculated(JSONObject obj) {
obj.
}然后我有新来的json
{"params":{"date_a":"2017-05-01","date_b":"2017-05-02"}}但是在java中,obj.只给了我toString()和toJSONString()的选项,同时提供了所有的教程,很少有线程清楚地告诉我,我应该能够做obj.getJSONObject("params") --为什么会这样?我如何访问我的参数?
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>是依赖关系。
发布于 2018-11-23 07:38:29
json-simple库有get(String name)方法,需要像下面这样的外部类型浇铸
String name = (String) jsonObject.get("name");
JSONArray msg = (JSONArray) jsonObject.get("messages");
long age = (Long) jsonObject.get("age");但是gson库有预定义的方法这里
public JsonObject getAsJsonObject(String memberName)
public JsonArray getAsJsonArray(String memberName)Maven依赖项
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.3</version>
</dependency>发布于 2018-11-23 07:48:12
你可以从你的助手那里得到JSONString
Gson gson = (new GsonBuilder()).create();
jsonString = obj.get("params").getAsString();
JsonObject param= gson.fromJson(jsonString, JsonObject.class);正如Deadpool所说,您希望使用gson,但是添加了json-simple依赖项。
https://stackoverflow.com/questions/53442403
复制相似问题