在build.gradle里添加fastjson的依赖
implementation 'com.alibaba:fastjson:1.2.21'
有json格式的字符串:
{
"code": 200,
"message": "success",
"data": {
"a": "hello",
"b": 100
}
}
简单使用:转化为Map对象
JSONObject arr = JSONObject.parseObject(jsonData);
arr.get("code");
转换为Class对象
推荐使用
// Response是实体类,包含‘code’,‘message’,‘data’ 成员
Response response = JSON.parseObject(jsonData, Response.class);
// 获取成员
response.code;
response.getCode();