alibaba fastjson.JSON 取复杂的多层的方法

alibaba fastjson.JSON 取复杂的多层的方法_第1张图片

result接受的字符串:

 {"result": [{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{

"sheet_type":{"item_words":"发票联","chinese_key":"联次","item_score":1.0,"item_quad":[1474,282,1811,282,1811,359,1474,359]},

"date":{"item_words":"2018年03月27日","chinese_key":"开票日期","item_score":1.0,"item_quad":[2645,302,2997,302,2997,356,2645,356]},

},"type_msg":"增值税普通发票"

}]}

JSONObject jsonobj2 = JSON.parseObject(result); //将json字符串转换成jsonObject对象
System.out.println("1============="+jsonobj2.get("result"));
结果:1=============[{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}]

System.out.println("2============="+jsonobj2.getString("result"));
结果:2=============[{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}]

System.out.println("3============="+jsonobj2.getJSONArray("result")); 
结果:1=============[{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}]

System.out.println("4============="+jsonobj2.getJSONArray("result").getJSONObject(0)); 
结果:{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}

System.out.println("5============="+jsonobj2.getJSONArray("result").get(0));
结果:{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}

System.out.println("6============="+jsonobj2.getJSONArray("result").getString(0));
结果:{"expense_type":"4","error_msg":"识别成功","pass_fee":0,"expense_type_msg":"餐饮","recognize_result":{"sheet_type":{"item_words":"发票联"}...}}

System.out.println("7============"+jsonobj2.getJSONArray("result").getJSONObject(0).getString("expense_type_msg"));
7=============餐饮
System.out.println("8============="+jsonobj2.getJSONArray("result").getJSONObject(0).getJSONObject("recognize_result").getJSONObject("sheet_type").getString("item_words"));
8=============发票联

 

System.out.println("9============="+jsonobj2.getJSONArray("result").getJSONObject(0).getJSONObject("recognize_result").getJSONObject("sheet_type").getString("item_words")); 
9=============发票联

System.out.println("10============="+jsonobj2.getJSONArray("result").getJSONObject(0).getJSONObject("recognize_result").getJSONObject("sheet_type").getJSONArray("item_quad")); 

10=============[1481,308,1813,308,1813,384,1481,384]

JSONObject的get(“”)方法、gettostring()方法、getJSONArray获取的是[{{...},{...})]                                 

  getJSONArray()后继续跟方法如getJSONObject(0)、get(0)、getString(0)取到{{...},{....}}

 

运用好getstring,get(0),getJSONObject(0),getJSONArray就可以在复杂的层次里取值

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(json)