使用第三方庫com.alibaba.fastjson解析,依賴包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
Json的類型
JSON 值可以是:
- 數字(整數或浮點數)
- 字符串(在雙引號中)
- 邏輯值(true 或 false)
- 數組(在中括號中)
- 對象(在大括號中)
- null
Java解析Json對象
json模板數據
{ "person": { "id": "123", "name": "name", "address": "address" } }
使用
jsonObject.getJSONObject
使用第三方庫,解析代碼如下:
@Test public void testJsonObject(){ String jsonString = "{\n" + " \"person\":{\n" + " \"id\":\"123\",\n" + " \"name\":\"name\",\n" + " \"address\":\"address\"\n" + " }\n" + "}"; try { JSONObject jsonObject = JSONObject.parseObject(jsonString); JSONObject personObject = jsonObject.getJSONObject("person"); if(personObject!=null){ Person person = new Person(); person.setId(personObject.getString("id")); person.setName(personObject.getString("name")); person.setAddress(personObject.getString("address")); System.out.println(person); } } catch (Exception e) { e.printStackTrace(); } }
運行結果如下:

Java解析Array數組
模板數據:
{ "students": [ "xiaoming", "xiaohua", "daming", "dahua" ] }
使用
jsonObject.getJSONArray
使用第三方庫,解析代碼如下:
@Test public void testJsonArray(){ String jsonString = "{\n" + " \"students\": [\n" + " \"xiaoming\",\n" + " \"xiaohua\",\n" + " \"daming\",\n" + " \"dahua\"\n" + " ]\n" + "}"; try { JSONObject jsonObject = JSONObject.parseObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("students"); for (int i = 0; i <jsonArray.size(); i++) { System.out.println(jsonArray.get(i)); } } catch (Exception e) { e.printStackTrace(); } }
運行結果如下:

Json解析ArrayObject數組對象
模板數據:
{ "persons": [ { "id": "1", "name": "xiaoming", "address": "address" }, { "id": "2", "name": "xiaohua", "address": "address" }, { "id": "3", "name": "daming", "address": "address" }, { "id": "4", "name": "dahua", "address": "address" } ] }
使用第三方庫,解析代碼如下:
@Test public void testJsonArrayAndObject(){ String jsonString = "{\n" + " \"persons\": [\n" + " {\n" + " \"id\": \"1\",\n" + " \"name\": \"xiaoming\",\n" + " \"address\": \"address\"\n" + " },\n" + " {\n" + " \"id\": \"2\",\n" + " \"name\": \"xiaohua\",\n" + " \"address\": \"address\"\n" + " },\n" + " {\n" + " \"id\": \"3\",\n" + " \"name\": \"daming\",\n" + " \"address\": \"address\"\n" + " },\n" + " {\n" + " \"id\": \"4\",\n" + " \"name\": \"dahua\",\n" + " \"address\": \"address\"\n" + " }\n" + " ]\n" + "}"; try { JSONObject jsonObject = JSONObject.parseObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("persons"); for (int i = 0; i <jsonArray.size(); i++) { JSONObject jsonArrayObjectItem = JSONObject.parseObject(jsonArray.get(i).toString()); if(jsonArrayObjectItem!=null){ Person person = new Person(); person.setId(jsonArrayObjectItem.getString("id")); person.setName(jsonArrayObjectItem.getString("name")); person.setAddress(jsonArrayObjectItem.getString("address")); System.out.println(person); } } } catch (Exception e) { e.printStackTrace(); } }
解析結果如下:

下面來個小練習,可以下載這個地址的文件:簡單的練習
鏈接:https://pan.baidu.com/s/1CWReej0FPmI1IHketA4Yxg 密碼:5oy4
大家可以解析一下,練練手,這里就不公布答案了。以后有時間補上答案。我要求返回的數據格式是這樣的。
{ "sheet_num": "100", "result": [ { "key?": "result", "NoNodeId": "?", "nodeId": "?" } ], "desc": "所有的{nodeId}相加", "Nodesc": "所有的{NoNodeId}相加", "text": "{desc} \n {Nodesc}" }
key?即取所有value等於result的key,
取所有NoNodeId的值,並存儲數組里面。
取所有nodeId的值,並存儲數組里面。
desc 就是所有的nodeId相加
Nodesc 就是所有的NoNodeId相加
text,就是desc加上回車,再加Nodesc
