json的常见转换

1. 常规json转换

{
    "list": [
        {
            "model": "id",
            "jude": "ne",
            "input": "BOND"
        },
        {
            "model": "a_pande_time",
            "jude": "gt",
            "time": "2023-10-09"
        }
    ]
}
public static void main(String[] args) throws IOException {
        String model = "";
        String jude = "";
        String input = "";
        String time = "";
        String jsonString = "{\n" +
                "    \"list\": [\n" +
                "        {\n" +
                "            \"model\": \"id\",\n" +
                "            \"jude\": \"ne\",\n" +
                "            \"input\": \"BOND\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"model\": \"a_pande_time\",\n" +
                "            \"jude\": \"gt\",\n" +
                "            \"time\": \"2023-10-09\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        JSONObject  jsonObject = JSON.parseObject(jsonString);
        String list = jsonObject.getString("list");
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonArray = objectMapper.readTree(list);
        for (JsonNode jsonNode : jsonArray) {
            model = jsonNode.get("model") == null ? "" : jsonNode.get("model").asText();
            jude = jsonNode.get("jude") == null ? "" : jsonNode.get("jude").asText();
            input = jsonNode.get("input") == null ? "" : jsonNode.get("input").asText();
            time = jsonNode.get("time") == null ? "" : jsonNode.get("time").asText();
            System.out.println("model: " + model + ", jude: " + jude + ", input: " + input + ", time: " + time);
        }

    }
model: id, jude: ne, input: BOND, time: 
model: a_pande_time, jude: gt, input: , time: 2023-10-09

2.json数组转换为实体类

{
    "list": [
        {
            "id": "1",
            "word": "id",
            "count": "14"
        },
        {
           "id": "2",
            "word": "name",
            "count": "15"
        }
    ]
}
public static void main(String[] args) throws IOException {
        String model = "";
        String jude = "";
        String input = "";
        String time = "";
        String jsonString = "{\n" +
                "    \"list\": [\n" +
                "        {\n" +
                "            \"id\": \"1\",\n" +
                "            \"word\": \"id\",\n" +
                "            \"count\": \"14\"\n" +
                "        },\n" +
                "        {\n" +
                "           \"id\": \"2\",\n" +
                "            \"word\": \"name\",\n" +
                "            \"count\": \"15\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        JSONObject  jsonObject = JSON.parseObject(jsonString);
        String list = jsonObject.getString("list");
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonArray = objectMapper.readTree(list);
        for (JsonNode jsonNode : jsonArray) {
            Demo demo = JSON.parseObject(jsonNode.toString(),Demo.class);
            System.out.println(demo.toString());
        }

    }
Demo{id='1', word='id', count='14'}
Demo{id='2', word='name', count='15'}

你可能感兴趣的:(Java,java)