alibaba.fastjson.JSONObject.parseObject的使用
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,当你要处理一段接收的json数据,就可以考虑使用JSONObject.parseObject
import com.alibaba.fastjson.JSONObject;
parseObject(String str)的作用
JSON.parseObject(String str)是将str转化为相应的JSONObject对象,其中str是“键值对”形式的json字符串,转化为JSONObject对象之后就可以使用其内置的方法,进行各种处理了。
json字段
{
"code":0,
"data":{
"city":{
"cityId":284609,
"counname":"中国",
"ianatimezone":"Asia/Shanghai",
"name":"东城区",
"pname":"北京市",
"secondaryname":"北京市",
"timezone":"8"
},
"liveIndex":{
"2021-03-17":[
{
"code":5,
"day":"2021-03-17",
"desc":"天气较好,路面干燥,交通气象条件良好,车辆可以正常行驶。",
"level":"1",
"name":"交通指数",
"status":"良好",
"updatetime":"2021-03-17 06:24:03"
},
{
"code":14,
"day":"2021-03-17",
"desc":"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。",
"level":"1",
"name":"旅游指数",
"status":"适宜",
"updatetime":"2021-03-17 06:24:03"
}
]
}
},
"msg":"success",
"rc":{
"c":0,
"p":"success"
}
}
{
"code":0,
"data":{
"aqi":{
"cityName":"东城区",
"co":"8",
"coC":"0.8",
"no2":"28",
"no2C":"56.0",
"o3":"2",
"o3C":"6.0",
"pm10":"160",
"pm10C":"270.0",
"pm25":"73",
"pm25C":"53.0",
"pubtime":"1615939200000",
"rank":"527/678",
"so2":"1",
"so2C":"3.0",
"value":"160"
},
"city":{
"cityId":284609,
"counname":"中国",
"ianatimezone":"Asia/Shanghai",
"name":"东城区",
"pname":"北京市",
"secondaryname":"北京市",
"timezone":"8"
}
},
"msg":"success",
"rc":{
"c":0,
"p":"success"
}
}
String json = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(json);
JSONObject date = jsonObject.getJSONObject("data");
JSONObject liveIndex = date.getJSONObject("liveIndex");
for (Map.Entry<String, Object> entry : liveIndex.entrySet()) {
JSONArray jsonArray = liveIndex.getJSONArray(entry.getKey());
for (Object object : jsonArray) {
JSONObject object1 = (JSONObject) object;
String name = object1.getString("code");
String level = object1.getString("level");
hashMap.put(name,level);
}
}
String json = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(json);
JSONObject data = jsonObject.getJSONObject("data");
JSONObject api = data.getJSONObject("aqi");
String pm25C = api.getString("pm25C");
JSON.parseObject(String str)与JSONObject.parseObject(String str)的区别
根据源码显示:JSON是一个抽象类,JSON中有一个静态方法parseObject(String text),将text解析为一个JSONObject对象并返回;JSONObject是一个继承自JSON的类,当调用JSONObject.parseObject(result)时,会直接调用父类的parseObject(String text)。所以两者没什么区别,一个是用父类去调用父类自己的静态的parseObject(String text),一个是用子类去调用父类的静态parseObject(String text),两者调的是同一个方法。
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献2条内容
所有评论(0)