json字符串转map、json数组演示

公司项目用的IBM封装的json解析,此处采用阿里的fastjson进行演示,代码如下:

 1 package com.alphajuns.test;
 2 
 3 import com.alibaba.fastjson.JSON;
 4 import com.alibaba.fastjson.JSONArray;
 5 import com.alibaba.fastjson.JSONObject;
 6 import org.junit.Test;
 7 
 8 import java.util.Map;
 9 
10 /**
11  * @ClassName JsonToMapTest
12  * @Description
13  * @Author AlphaJunS
14  * @Date 2019/10/23 22:34
15  * @Version V1.0
16  **/
17 public class JsonToMapTest {
18 
19     /**
20      * @description json字符串转json数组
21      * @author AlphaJunS
22      * @date 2019/10/23
23      * @param []
24      * @return void
25      */
26     @Test
27     public void run3() {
28         // json字符串
29         String jsonStr = "[{\"ID\":\"SX5-1001432\",\"PROCESSID\":\"OP2080\"}, "
30                 + "{\"ID\":\"SX5-1601110\",\"PROCESSID\":\"OP2150\"}, "
31                 + "{\"ID\":\"X03004-1023111\",\"PROCESSID\":\"OP1140\"}, "
32                 + "{\"ID\":\"X03004-1003012\",\"PROCESSID\":\"GOP260\"}, "
33                 + "{\"ID\":\"7903075118\",\"PROCESSID\":\"GOP260\"},{\"ID\":\"13MA-11011\"}]";
34         JSONArray jsonArray = JSON.parseArray(jsonStr);
35         // 遍历打印
36         printMethod(jsonArray);
37     }
38 
39     /**
40      * @description
41      * @author AlphaJunS
42      * @date 2019/10/23
43      * @param []
44      * @return void
45      */
46     @Test
47     public void run2() {
48         // json字符串
49         String jsonStr = "{\"OPERATION\":[{\"ID\":\"SX5-1001432\",\"PROCESSID\":\"OP2080\"}, "
50                 + "{\"ID\":\"SX5-1601110\",\"PROCESSID\":\"OP2150\"}, "
51                 + "{\"ID\":\"X03004-1023111\",\"PROCESSID\":\"OP1140\"}, "
52                 + "{\"ID\":\"X03004-1003012\",\"PROCESSID\":\"GOP260\"}, "
53                 + "{\"ID\":\"7903075118\",\"PROCESSID\":\"GOP260\"},{\"ID\":\"13MA-11011\"}]}";
54 
55         // json转map
56         Map map = (Map) JSON.parse(jsonStr);
57         System.out.println("map的value类型:" + map.get("OPERATION").getClass());
58         JSONArray jsonArray = (JSONArray) map.get("OPERATION");
59         printMethod(jsonArray);
60     }
61 
62     /**
63      * @description json字符串转map
64      * @author AlphaJunS
65      * @date 2019/10/23
66      * @param []
67      * @return void
68      */
69     @Test
70     public void run1() {
71         String jsonStr= "{\"OPERATION\":{\"ID\":\"SX5-1001432\",\"PROCESSID\":\"OP2080\"}}";
72         // json转map
73         Map map = (Map) JSON.parse(jsonStr);
74         System.out.println("map的value类型:" + map.get("OPERATION").getClass());
75         JSONObject jsonObject = (JSONObject) map.get("OPERATION");
76         System.out.println("ID:" + jsonObject.get("ID") + ",PROCESSID:" + jsonObject.get("PROCESSID"));
77     }
78 
79     /**
80      * @description 遍历json数组打印
81      * @author AlphaJunS
82      * @date 2019/10/23
83      * @param [jsonArray]
84      * @return void
85      */
86     public void printMethod(JSONArray jsonArray) {
87         // 遍历json数组
88         for (int i = 0; i < jsonArray.size(); i++) {
89             JSONObject jsonObject = (JSONObject) jsonArray.get(i);
90             System.out.println("第" + (i + 1) + "个对象:");
91             System.out.println("ID:" + jsonObject.get("ID") + ",PROCESSID:" + jsonObject.get("PROCESSID"));
92         }
93     }
94 }
相关文章
相关标签/搜索