1. com.alibaba.fastjson
jar package address: https: //www.mvnjar.com/com.alibaba/fastjson/jar.html
The multilayer analytical
java resolves the following two strings json
Package jansonDemo; Import com.alibaba.fastjson.JSON; Import com.alibaba.fastjson.JSONArray; Import com.alibaba.fastjson.JSONObject; public class TestJSON { / ** * the JSON actually key-value pair ( "key": "value") * Key must be a string, value may be valid JSON data type (string, number, object, array, Boolean, or null) * If a string value, with jsonobj.getString ( "key") Get * value for numbers, a method using jsonobj.getIntValue ( "key"), jsonobj.getFloatValue ( "key"), jsonobj.getInteger ( "key") and basic data types like packaging Get * value if the Boolean value, with jsonobj.getBoolean ( "key"), jsonobj.getBooleanValue ( "key") acquisition * value if it is an array, with jsonobj.getJSONArray ( "key") Gets * value if the Object object is to obtain a jsonobj.get ( "key"), If the object is JSONObject * value, acquired by jsonobj.getJSONObject ( "key") * / / ** * This method is used to convert the string to an existing json json object and remove the object corresponding to the corresponding key value value * converted into a character string existing jsonobject, with JSON.parseObject (jsonStr) method * is as long as JSON {} represents a JSONObject, [] represents the JSONArray a * Gets an object with JSONObject JSONObject jsonobject.getJSONObject ( "key ") method * Gets an object with JSONArray JSONObject jsonobject.getJSONArray (" key ") method * / Private static void strWritedToJSONObject () { // a nested json object following is a child object json {\ n String myJsonObj =" " + "\" name \ ": \" runoob \ ", \ the n-" + "\" ALEXA \ ": 10000, \ the n-"+ " \"sites\": {\n" + "\" Site1 \ ": \" www.runoob.com \ ", \ the n-" + "\" Site2 \ ": \" m.runoob.com \ ", \ the n-" + "\" site3 \ ": \ "c.runoob.com \" \ n-"+ "} \ n-"+ "} " ; JSONObject jsonobj = JSON.parseObject (myJsonObj); // convert the string into json jsonObject objects / ** * Get each JSONObject a key value corresponding to the value when, according to what type of actual scene want to use less of each method ** * / System.out.println (jsonobj.get ( "name")); // removed name corresponding to the value the value obtained is a Object System.out.println (jsonobj.getString ( "name"));// remove the value corresponding to the value of the name, a String is obtained System.out.println (jsonobj.getIntValue ( "ALEXA")); // removed name value corresponding to the value obtained is a int System.out.println (jsonobj.get ( "sites")); // remove sites value corresponding to a value obtained an Object System.out.println (jsonobj.getString ( "sites" )); System.out.println (jsonobj.getJSONObject ( ; "sites")) // value corresponding to the value taken sites to give a child object JSONObject System.out.println (jsonobj.getJSONObject ( "sites") the getString ( "site2").); // remove JSONObject nested child object value corresponding to the value site2, must getJSONObject () first get JSONObject / ** * the following is a json object contains an array, the array also contains sub-objects and sub-json array * / 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" + "}"; System.out.println (jsonobj2.getString ()); System.out.println (jsonobj2.get ( "sites"convert the string into json jsonObject objects//= JSON.parseObject (myJsonObj2); Jsonobj2 the JSONObject//"sites")); System.out.println (jsonobj2.getJSONArray ());"sites" remove the value corresponding to the value of the sites, the object to obtain a JSONOArray // System.out.println (jsonobj2.getJSONObject ( "sites" )); this method can not be used, because the target sites is a JSONOArray // remove sites json object corresponding to a first value in the array json child objects . System.out.println (jsonobj2.getJSONArray ( "sites" ) getJSONObject (0 )); // get the result: { "name": "Google ", "info": [ "Android", "Google Search", "Google Translate"]} System.out.println (jsonobj2.getJSONArray ( "sites" ) .get (0 )); System.out.println (jsonobj2.getJSONArray ( "sites") the getString (0. )); // remove json object info sites corresponding to the value corresponding to the array of nested child objects below the first sub array json System.out.println (jsonobj2.getJSONArray ( "sites") .getJSONObject(0).getJSONArray("info")); //Results obtained: [ "Android", "Google Search", "Google Translate"] // remove the child object following the first json json object info sites in the corresponding array corresponding to the array of nested sub-second value System.out .println (. jsonobj2.getJSONArray ( "sites") getJSONObject (0) .getJSONArray ( "info") the getString (. 1).); // get the results: Google Search // sequentially json object takes a value corresponding to the array of sites array = jsonobj2.getJSONArray the JSONArray ( "sites" ); getJsonArrayItem (array); // sequentially remove the corresponding sites json object json second sub-array corresponding to the info array values below the object nested sub JSONArray arr = jsonobj2.getJSONArray ( "sites") getJSONObject (. 1) .getJSONArray ( "info." ); getJsonArrayItem(arr); } / ** * Manually add objects to a the 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","www.alibaba.com"); JSONObject jsonArrayObject2 = new JSONObject(); jsonArrayObject2.put("name","baidu"); jsonArrayObject2.put("info","www.baidu.com"); 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\":\"www.alibaba.com\"\n" + " },\n" + " {\n" + " \"name\":\"baidu\",\n" + " \"info\":\"www.baidu.com\"\n" + " }\n" + " ]"; JSONArray array = JSON.parseArray(arrayStr); System.out.println(array); (forgetJsonArrayItem (JSONArray Array) { voidstaticPrivate* / * values sequentially taken in JSONArray/ ** } int i=0; i<array.size(); i++) { System.out.println(array.get(i)); } } //测试类 public static void main(String[] args) { strWritedToJSONObject(); //writeStrToJSONObject(); //strToJsonArray(); } }
Summary: If {} is used getJSONObject [] with getJSONArray
{}, For use in the loop through JSONArray:
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 determines whether a string contains a json key value
public static void main (String [] args) throws JSONException { String jsonStr = "{ 'Content': [ '', '', '', ''], 'Baseline': { 'Content': [ '. 1', '2'], 'BaselineName': 'JC', 'BaselineId': '813xxx'}} " ; JSONObject jsonObject = new new JSONObject (jsonStr); // call has JSONObject methods herein to determine whether there is a key value, if returns presence to true IF (jsonObject .has ( "Baseline" )) { // here after the key operation corresponding to the current value of the presence of the jSONObject jsonObject2 = (the jSONObject) jsonObject.get ( "Baseline" ); ...... ... ... } }
How 4.fastjson judge JSONObject and 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); the JSONArray) {the instanceof(JSONIFthe else }Analytical itself to// JSONArray the JSONArray = (the JSONArray) JSON; System.out.println (JSONArray); // parsing can be self } }
These are the problems encountered by the project, thanks to the big brother to share:
https://www.cnblogs.com/janson071/p/9646678.html
https://blog.csdn.net/kxj19980524/article/details/83718971
https://www.cnblogs.com/1012hq/p/11141932.html
https://www.cnblogs.com/biehongli/p/9721846.html