JSON与String互转 以及两种JSON里的(数组和对象)的数据获取

15 篇文章 1 订阅
3 篇文章 1 订阅

目录

数据转换:

String 转 JSON :

JSON 转 String:

JSON里的数组获取

方法1:使用JSONbject类。

方法2:使用Jackson库将字符串转换为JsonNode对象:

JSON里的对象获取

方法1:使用JSONbject类。

方法2:使用Jackson库将字符串转换为JsonNode对象:


数据转换:

String 转 JSON :

 String result =  "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"查询成功\",\n" +
                "  \"result\": {\n" +
                "    \"records\": [\n" +
                "      {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      },\n" +
                "    {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"total\": 1,\n" +
                "    \"size\": 10,\n" +
                "    \"current\": 1,\n" +
                "    \"pages\": 1\n" +
                "  }\n" +
                "}";

JSONObject jsonObject = JSON.parseObject(result);

JSON 转 String:

String result =  "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"查询成功\",\n" +
                "  \"result\": {\n" +
                "    \"records\": [\n" +
                "      {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      },\n" +
                "    {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"total\": 1,\n" +
                "    \"size\": 10,\n" +
                "    \"current\": 1,\n" +
                "    \"pages\": 1\n" +
                "  }\n" +
                "}";
        try {

JSONObject jsonObject = JSON.parseObject(result);
String result2 = JSON.toJSONString(jsonObject);

JSON里的数组获取

方法1:使用JSONbject类。
public static void main(String[] args) {
        String result =  "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"查询成功\",\n" +
                "  \"result\": {\n" +
                "    \"records\": [\n" +
                "      {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      },\n" +
                "    {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"total\": 1,\n" +
                "    \"size\": 10,\n" +
                "    \"current\": 1,\n" +
                "    \"pages\": 1\n" +
                "  }\n" +
                "}";

        try {
            JSONObject jsonObject = new JSONObject(result);

            JSONObject resultObject = jsonObject.getJSONObject("result");
            JSONArray recordsArray = resultObject.getJSONArray("records");

            for (int i = 0; i < recordsArray.size(); i++) {
                JSONObject recordObject = recordsArray.getJSONObject(i);

                String jobName = recordObject.getString("jobName");
                String jobExperience = recordObject.getString("jobExperience");
                String education = recordObject.getString("education");

                System.out.println("Job Name: " + jobName);
                System.out.println("Job Experience: " + jobExperience);
                System.out.println("Education: " + education);
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
方法2:使用Jackson库将字符串转换为JsonNode对象:
public static void main(String[] args) {
        String result =  "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"查询成功\",\n" +
                "  \"result\": {\n" +
                "    \"records\": [\n" +
                "      {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      },\n" +
                "    {\n" +
                "        \"jobName\": \"高级开发工程师\",\n" +
                "        \"jobExperience\": \"一年以下\",\n" +
                "        \"education\": \"本科\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"total\": 1,\n" +
                "    \"size\": 10,\n" +
                "    \"current\": 1,\n" +
                "    \"pages\": 1\n" +
                "  }\n" +
                "}";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(result);

            JsonNode recordsNode = jsonNode.get("result").get("records");
            for (JsonNode recordNode : recordsNode) {
                String jobName = recordNode.get("jobName").asText();
                String jobExperience = recordNode.get("jobExperience").asText();
                String education = recordNode.get("education").asText();

                System.out.println("Job Name: " + jobName);
                System.out.println("Job Experience: " + jobExperience);
                System.out.println("Education: " + education);
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

JSON里的对象获取

方法1:使用JSONbject类。
public class Main {
    public static void main(String[] args) {
        String jsonString = "{\"name\": \"John\", \"age\": 30}";

        try {
            JSONObject jsonObject = new JSONObject(jsonString);

            String name = jsonObject.getString("name");
            int age = jsonObject.getInt("age");

            System.out.println("Name: " + name); // 输出: John
            System.out.println("Age: " + age);   // 输出: 30
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
方法2:使用Jackson库将字符串转换为JsonNode对象:
public class Main {
    public static void main(String[] args) {
        String jsonString = "{\"name\": \"John\", \"age\": 30}";

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(jsonString);

            // 可以使用jsonNode进行进一步的处理
            System.out.println(jsonNode.get("name").asText()); // 输出: John
            System.out.println(jsonNode.get("age").asInt());   // 输出: 30
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值