json string transfer map, json array demo

The company's project with IBM package json resolution, adopted here Ali fastjson presentation, as follows:

 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 traversing print json array
 81       * @author AlphaJunS
 82       * @date 2019/10/23
 83       * @param [JSONArray]
 84       * @return void
 85       * / 
86      public  void printMethod is (the JSONArray JSONArray) {
 87          // iterate json array 
88          for ( int I = 0; I <jsonArray.size (); I ++ ) {
 89              the jSONObject jsonObject = (the jSONObject) jsonArray.get (I);
 90              System.out.println ( "first" + ( i + 1) + "object:");
91             System.out.println("ID:" + jsonObject.get("ID") + ",PROCESSID:" + jsonObject.get("PROCESSID"));
92         }
93     }
94 }

 

Guess you like

Origin www.cnblogs.com/alphajuns/p/11729832.html