使用hutool解析json
注意点:
1.数组和字典接受的对象和方法不一样
数组使用JSONArray
字典使用JSONObject
2.如果字典中提取不存在的key,返回的结果null
package cn.npsel.test;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
public class TestHutoolJson {
public static void main(String[] args) {
String mStr = "[ { \"name\": \"海运运价\", \"desc\": \"海运实时运价\" }, { \"name\": \"运价\", \"desc\": \"线上运价\", \"img\": \"/images/index/spot.png\" } ]";
System.out.println(mStr);
JSONArray arr = JSONUtil.parseArray(mStr);
for (Object object : arr) {
JSONObject json = JSONUtil.parseObj(object);
System.out.println(json.getStr("name"));
System.out.println(json.getStr("desc"));
System.out.println(json.getStr("img"));
}
}
}
标签:java,cn,System,hutool,json,println,out
From: https://blog.csdn.net/awonw/article/details/140648091