关于java数据样品以及转换

发布于:2024-04-20 ⋅ 阅读:(26) ⋅ 点赞:(0)

关于集合

List<String>

List<String> list = duoMapper.selectName();

[
  "alice1",
  "alice2",
  "alice3",
  "alice4",
  "alice5",
  "alice6",
  "alice7",
  "alice8",
  "alice9",
  "alice10"
]

List<UserInfo>

List<UserInfo> list = duoMapper.selectName();
[
  {
    "id": null,
    "name": "alice1",
    "age": 24,
    "email": null,
    "area": null
  },
  {
    "id": null,
    "name": "alice2",
    "age": 27,
    "email": null,
    "area": null
  }
]

Map<String,Object>

map格式必须是两个,而且只能查一条数据,key若重复,将覆盖value

Map<String,Object> map = duoMapper.selectName();

select name,age from user_info limit 1 

{
  "name": "alice1",
  "age": 24

LinkedHashMap是hashmap的子类,都属于map,数据格式一致

关于数组

        // 假设我们有一个整型数组
        int[] numbers = {1, 2, 3, 4, 5};

        // 使用for循环遍历数组并打印每个元素
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i]);
        }

12345

二维的话,双层

        // 二维数组
        int[][] matrix = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
        };

        // 外层循环遍历行
        for (int i = 0; i < matrix.length; i++) {
            // 内层循环遍历列
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println(); // 每打印完一行数据后换行
        }

1 2 3 
4 5 6 
7 8 9 

没有任何符号包裹,裸数据

 关于json

导入jar包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.33</version>
</dependency>

JSONObject

 一种键值对的集合,写法类似map,所以key重复,value被覆盖,只有一组

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","111");
        jsonObject.put("bbb","222");
        System.out.println(jsonObject);

{"aaa":"111","bbb":"222"}

JSONArray

一个

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","111");
        jsonObject.put("bbb","222");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        System.out.println(jsonArray);

[{"aaa":"111","bbb":"222"}] 

多个

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","111");
        jsonObject.put("bbb","222");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("ccc","333");
        jsonObject2.put("ddd","444");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject2);
        System.out.println(jsonArray);

[{"aaa":"111","bbb":"222"},{"ccc":"333","ddd":"444"}] 

关于json转换

array转json

JSONObject jsonObject3 = (JSONObject)jsonArray.get(i);
JSONObject jsonObject4 = jsonArray.getJSONObject(i);

array转string

    @Test
    public void test3(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aaa","111");
        jsonObject.put("bbb","222");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        //array转string
        String str = JSONObject.toJSONString(jsonArray, SerializerFeature.WriteMapNullValue);
        System.out.println(str);
    }

[{"aaa":"111","bbb":"222"}] 

json转string

        // 创建一个JSONObject对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "John Doe");
        jsonObject.put("age", 30);
        jsonObject.put("city", "New York");

        // 将JSONObject转换成字符串
        String jsonString = jsonObject.toString();

        // 输出转换后的字符串
        System.out.println(jsonString);

{"city":"New York","name":"John Doe","age":30} 

string转json

        String result =  "{\"code\":200,\"message\":\"success\"}";
        JSONObject json = JSONObject.parseObject(result);
        System.out.println(json);

{"code":200,"message":"success"}

如果String有多组数据

String str = "[{\"name\":\"Alice\",\"age\":20},{\"name\":\"Bob\",\"age\":30}]";
        JSONArray jsonArray = JSON.parseArray(str);
        System.out.println(jsonArray);

[{"name":"Alice","age":20},{"name":"Bob","age":30}] 

json转java对象

        String result =  "{\"id\":200,\"name\":\"jogging\"}";
        JSONObject jsonObject = JSONObject.parseObject(result);
        UserInfo userInfo = JSON.parseObject(jsonObject.toString(), UserInfo.class);
        System.out.println(userInfo);

UserInfo(id=200, name=jogging, age=null, email=null, area=null) 


网站公告

今日签到

点亮在社区的每一天
去签到