zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package testjson;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
import org.junit.Test;
 
public class JSONTest {
 
    /**
     * JSON
     */
    @Test
    public void testArray() {
 
        String[] arr = { "asd", "dfgd", "asd", "234" };
        JSONArray jsonarray = JSONArray.fromObject(arr);
        System.out.println(jsonarray);
 
    }
 
    // ����ת����JSON
    @Test
    public void testObjectToJSON() {
        UserInfo user = new UserInfo(1001, "得瑟得瑟");
        JSONArray jsonArray = JSONArray.fromObject(user);
        System.out.println(jsonArray);
    }
 
    // Mapת����json�� ����jsonObject
    @Test
    public void testMapToJSON() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", 1001);
        map.put("userName", "ss");
        map.put("userSex", "对对对");
        JSONObject jsonObject = JSONObject.fromObject(map);
        System.out.println(jsonObject);
    }
 
    // Listת����JSON
    @Test
    public void testListToJSON() {
 
        List<UserInfo> list = new ArrayList<UserInfo>();
        UserInfo user = new UserInfo(1001, "ss多少");
        list.add(user);
        list.add(user);
        list.add(user);
 
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray);
    }
    public static void main(String[] args ){
        JSONTest test = new JSONTest();
        test.testArray();
    }
}