一、使用jackson
String jsonData = "{\"identifier\":\"18111111111\",\"opType\":1,\"platform\":0}";
ObjectMapper mapper = new ObjectMapper();
try {
@SuppressWarnings("unchecked")
Map<String, Object> tmpMap=mapper.readValue(jsonData, Map.class);
String identifier=(String) tmpMap.get("identifier");
String identifier2=(String) tmpMap.get("identifier2");
int opType=(Integer) tmpMap.get("opType");
int platform=(Integer) tmpMap.get("platform");
System.out.println("identifier:"+identifier);
System.out.println("identifier2:"+identifier2);
System.out.println("opType:"+opType);
System.out.println("platform:"+platform);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
renderJson(jsonData);
注:https://blog.csdn.net/an129/article/details/74175665
二、使用gson
public class MyType<T> {
public T gsonToMap(String strJson) {
return new Gson().fromJson(strJson, new TypeToken<T>() {
}.getType());
}
}
public void gson() {
String json = "{\"identifier\":\"18111111111\",\"opType\":1,\"platform\":0}";
Map<String, Object> tmpMap = new MyType<Map<String, Object>>().gsonToMap(json);
String identifier=(String) tmpMap.get("identifier");
String identifier2=(String) tmpMap.get("identifier2");
double opType=(Double) tmpMap.get("opType");
double platform=(Double) tmpMap.get("platform");
System.out.println("identifier:"+identifier);
System.out.println("identifier2:"+identifier2);
System.out.println("opType:"+opType);
System.out.println("platform:"+platform);
renderJson(json);
}
注:https://juejin.im/post/5cbb3c5af265da03ab23258c
只要是Number(包括int、long、float、double等)型,都会被强制转化成double,至于为什么这么做,因为这里所有的类型都可以转换成double,而反过来则不行。
三、前后端开发json Api设计规范总结
注:https://sobird.me/http-json-api-guide.htm
http://tutuge.me/2016/05/02/design-json-api-respoense/
https://crifan.github.io/http_restful_api/website/restful_experience/pagination.html
四、json数据参考
{
"errcode": 0,
"errmsg": "ok",
"result": {
"next_cursor": 2,
"process_list": [
{
"icon_url": "https://gw.xxxx/T-102-102.png",
"name": "物品领用",
"process_code": "PROC-YMLA1-xxxx-11WFJ-1",
"url": "https://aflow.dingtalk.com/dingtalk/mobile/homepage.htm?xxxx"
},
{
"icon_url": "https://gw.xxxx/T-102-103.png",
"name": "通用审批",
"process_code": "PROC-YMLA1-xxxx-11WFJ-2",
"url": "https://aflow.dingtalk.com/dingtalk/mobile/homepage.htm?xxxx"
}
]
},
"request_id": "72hy9suzb5cg"
}
https://developers.dingtalk.com/document/app/you-can-call-this-operation-to-retrieve-a-list-of
{
"state": "fail"
}
如果错误返回-1 ,fail