java解析多层嵌套json字符串

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java解析多层嵌套json字符串
1. com.alibaba.fastjson
2.多层解析
java分别解析下⾯两个json字符串
package jansonDemo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class TestJSON {
/**
* JSON实际上也是键值对("key":"value")
* key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)
* value如果是字符串,⽤jsonobj.getString("key")获取
* value如果是数字,⽤jsonobj.getIntValue("key"),jsonobj.getFloatValue("key"),jsonobj.getInteger("key")等基本数据类型及其包装类的⽅法获取
* value如果是布尔值,⽤jsonobj.getBoolean("key"),jsonobj.getBooleanValue("key")获取
* value如果是数组,⽤jsonobj.getJSONArray("key")获取
* value如果是Object对象,⽤jsonobj.get("key"),获取
* value如果是JSONObject对象,⽤jsonobj.getJSONObject("key")获取
*/
/**
* 该⽅法⽤于将已有的json字符串转换为json对象,并取出该对象中相应的key对应的value值
* 将已有的字符串转换成jsonobject,⽤JSON.parseObject(jsonStr)⽅法
* json中只要是{}就代表⼀个JSONObject,[]就代表⼀个JSONArray
* 获取JSONObject对象⽤JSONObject jsonobject.getJSONObject("key")⽅法
* 获取JSONArray对象⽤JSONObject jsonobject.getJSONArray("key")⽅法
*/
private static void strWritedToJSONObject() {
//以下是⼀个json对象中嵌套⼀个json⼦对象
String myJsonObj = "{\n" +
" \"name\":\"runoob\",\n" +
" \"alexa\":10000,\n" +
" \"sites\": {\n" +
" \"site1\":\"\",\n" +
" \"site2\":\"\",\n" +
" \"site3\":\"\"\n" +
" }\n" +
"}";
JSONObject jsonobj = JSON.parseObject(myJsonObj); //将json字符串转换成jsonObject对象
/***获取JSONObject中每个key对应的value值时,可以根据实际场景中想得到什么类型就分别运⽤不到的⽅法***/
System.out.println(jsonobj.get("name")); //取出name对应的value值,得到的是⼀个object
System.out.println(jsonobj.getString("name")); //取出name对应的value值,得到的是⼀个String
System.out.println(jsonobj.getIntValue("alexa")); //取出name对应的value值,得到的是⼀个int
System.out.println(jsonobj.get("sites")); //取出sites对应的value值,得到的是⼀个object
System.out.println(jsonobj.getString("sites"));
System.out.println(jsonobj.getJSONObject("sites")); //取出sites对应的value值,得到⼀个JSONObject⼦对象
System.out.println(jsonobj.getJSONObject("sites").getString("site2")); //取出嵌套的JSONObject⼦对象中site2对应的value值,必须⽤getJSONObject()先获取JSONObject /**
* 以下是⼀个json对象中包含数组,数组中⼜包含json⼦对象和⼦数组
*/
String myJsonObj2 = "{\n" +
" \"name\":\"⽹站\",\n" +
" \"num\":3,\n" +
" \"sites\": [\n" +
" { \"name\":\"Google\", \"info\":[ \"Android\", \"Google 搜索\", \"Google 翻译\" ] },\n" +
" { \"name\":\"Runoob\", \"info\":[ \"菜鸟教程\", \"菜鸟⼯具\", \"菜鸟微信\" ] },\n" +
" { \"name\":\"Taobao\", \"info\":[ \"淘宝\", \"⽹购\" ] }\n" +
" ]\n" +
"}";
JSONObject jsonobj2 = JSON.parseObject(myJsonObj2); //将json字符串转换成jsonObject对象
System.out.println(jsonobj2.get("sites"));
System.out.println(jsonobj2.getString("sites"));
System.out.println(jsonobj2.getJSONArray("sites")); //取出sites对应的value值,得到⼀个JSONOArray对象
//System.out.println(jsonobj2.getJSONObject("sites")); 不能⽤该⽅法,因为sites是⼀个JSONOArray对象
//取出json对象中sites对应数组中第⼀个json⼦对象的值
System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0)); //得到结果:{"name":"Google","info":["Android","Google 搜索","Google 翻译"]} System.out.println(jsonobj2.getJSONArray("sites").get(0));
System.out.println(jsonobj2.getJSONArray("sites").getString(0));
//取出json对象中sites对应数组中第⼀个json⼦对象下⾯info对应的嵌套⼦数组值
System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0).getJSONArray("info")); //得到结果:["Android","Google 搜索","Google 翻译"] //取出json对象中sites对应数组中第⼀个json⼦对象下⾯info对应的嵌套⼦数组中第⼆个值
System.out.println(jsonobj2.getJSONArray("sites").getJSONObject(0).getJSONArray("info").getString(1)); //得到结果:Google 搜索
//依次取出json对象中sites对应数组中的值
JSONArray array = jsonobj2.getJSONArray("sites");
getJsonArrayItem(array);
//依次取出json对象中sites对应数组中第⼆个json⼦对象下⾯info对应的嵌套⼦数组值
JSONArray arr = jsonobj2.getJSONArray("sites").getJSONObject(1).getJSONArray("info");
getJsonArrayItem(arr);
}
/**
* ⼿动添加对象到⼀个JSONObject
*/
private static void writeStrToJSONObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","tom");
jsonObject.put("age",20);
JSONArray jsonArray = new JSONArray();
JSONObject jsonArrayObject1 = new JSONObject();
jsonArrayObject1.put("name","alibaba");
jsonArrayObject1.put("info","");
JSONObject jsonArrayObject2 = new JSONObject();
jsonArrayObject2.put("name","baidu");
jsonArrayObject2.put("info","");
jsonArray.add(jsonArrayObject1);
jsonArray.add(jsonArrayObject2);
jsonObject.put("sites",jsonArray);
System.out.println(jsonObject);
}
/**
* 将字符串转为JSONArray
*/
private static void strToJsonArray() {
String arrayStr = "[\n" +
" {\n" +
" \"name\":\"alibaba\",\n" +
" \"info\":\"\"\n" +
" },\n" +
" {\n" +
" \"name\":\"baidu\",\n" +
" \"info\":\"\"\n" +
" }\n" +
" ]";
JSONArray array = JSON.parseArray(arrayStr);
System.out.println(array);
}
/**
* 依次取出JSONArray中的值
*/
private static void getJsonArrayItem(JSONArray array) {
for (int i=0; i<array.size(); i++) {
System.out.println(array.get(i));
}
}
//测试类
public static void main(String[] args) {
strWritedToJSONObject();
//writeStrToJSONObject();
//strToJsonArray();
}
}
⼩结:如果是{ } ⽤ getJSONObject [ ] ⽤ getJSONArray
JSONArray内的{ },⽤for循环遍历:
String s = "[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]"; JSONArray ja = jsonx.getJSONArray("data");
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = ja.getJSONObject(i);
String building_id = jo.getString("building_id");
}
3.JsonObject判断⼀个json串中是否含有某个key值
public static void main(String[] args) throws JSONException {
String jsonStr = "{'content':['','','',''],'Baseline':{'content':['1','2'],'BaselineName':'JC','BaselineId':'813xxx'}}";
JSONObject jsonObject = new JSONObject(jsonStr);
//这⾥调⽤JSONObject 的has⽅法来判断⼀个key值是否存在,如果存在返回true
if(jsonObject .has("Baseline")){
//这⾥对应当前key值存在后的操作
JSONObject jsonObject2 = (JSONObject)jsonObject.get("Baseline");
......
......
}
}
4.fastjson如何判断JSONObject和JSONArray
/**
* 单层的orgJson判断是否是JSONObject还是JSONArray.
*/
public static void simpleJSONObjectOrgJson() {
String message = "[{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }]";
Object json = new JSONTokener(message).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
System.out.println(jsonObject);
//⾃⾏解析即可
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
System.out.println(jsonArray);
//⾃⾏解析即可
}
}
以上是此次项⽬遇到的问题,感谢以下⼤佬分享:。

相关文档
最新文档