2025.5.29

Java 处理 JSON 字符串

要处理这个 JSON 字符串,你可以使用 Java 中的 JSON 库,如 org.jsoncom.google.gson。下面我将展示如何使用这两种库来处理这个 JSON 数据。

使用 org.json 库

首先,确保你添加了 org.json 库的依赖(Maven 或 Gradle)。

import org.json.JSONObject;

public class JsonProcessor {
    public static void main(String[] args) {
        String jsonStr = "{\n" +
                "      \"firstLinesChars\": 0,\n" +
                "      \"level\": 0,\n" +
                "      \"blocks\": [\n" +
                "        {\n" +
                "          \"text\": \"电话:123-4567-890\"\n" +
                "        },\n" +
                "        {\n" +
                "          \"text\": \"邮箱:zhangsan@example.com\"\n" +
                "        }\n" +
                "      ],\n" +
                "      \"markdownContent\": \"电话:123-4567-890邮箱:zhangsan@example.com  \\n\\n\",\n" +
                "      \"index\": 1,\n" +
                "      \"subType\": \"none\",\n" +
                "      \"lineHeight\": 12,\n" +
                "      \"text\": \"电话:123-4567-890邮箱:zhangsan@example.com\\n\",\n" +
                "      \"alignment\": \"left\",\n" +
                "      \"type\": \"text\",\n" +
                "      \"pageNum\": 0,\n" +
                "      \"uniqueId\": \"8412ee4f51eb01881c3a1c722a519c1c\"\n" +
                "    }";
        
        JSONObject jsonObject = new JSONObject(jsonStr);
        
        // 访问各个字段
        int firstLinesChars = jsonObject.getInt("firstLinesChars");
        String uniqueId = jsonObject.getString("uniqueId");
        String markdownContent = jsonObject.getString("markdownContent");
        
        System.out.println("Unique ID: " + uniqueId);
        System.out.println("Markdown Content: " + markdownContent.trim());
        
        // 处理 blocks 数组
        System.out.println("\nBlocks:");
        for (Object block : jsonObject.getJSONArray("blocks")) {
            JSONObject blockObj = (JSONObject) block;
            System.out.println(blockObj.getString("text"));
        }
    }
}

使用 Gson 库

如果你更喜欢使用 Google 的 Gson 库:

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GsonProcessor {
    public static void main(String[] args) {
        String jsonStr = "{\n" +
                "      \"firstLinesChars\": 0,\n" +
                "      \"level\": 0,\n" +
                "      \"blocks\": [\n" +
                "        {\n" +
                "          \"text\": \"电话:123-4567-890\"\n" +
                "        },\n" +
                "        {\n" +
                "          \"text\": \"邮箱:zhangsan@example.com\"\n" +
                "        }\n" +
                "      ],\n" +
                "      \"markdownContent\": \"电话:123-4567-890邮箱:zhangsan@example.com  \\n\\n\",\n" +
                "      \"index\": 1,\n" +
                "      \"subType\": \"none\",\n" +
                "      \"lineHeight\": 12,\n" +
                "      \"text\": \"电话:123-4567-890邮箱:zhangsan@example.com\\n\",\n" +
                "      \"alignment\": \"left\",\n" +
                "      \"type\": \"text\",\n" +
                "      \"pageNum\": 0,\n" +
                "      \"uniqueId\": \"8412ee4f51eb01881c3a1c722a519c1c\"\n" +
                "    }";
        
        JsonObject jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject();
        
        // 访问各个字段
        int firstLinesChars = jsonObject.get("firstLinesChars").getAsInt();
        String uniqueId = jsonObject.get("uniqueId").getAsString();
        String markdownContent = jsonObject.get("markdownContent").getAsString();
        
        System.out.println("Unique ID: " + uniqueId);
        System.out.println("Markdown Content: " + markdownContent.trim());
        
        // 处理 blocks 数组
        System.out.println("\nBlocks:");
        JsonArray blocks = jsonObject.getAsJsonArray("blocks");
        for (int i = 0; i < blocks.size(); i++) {
            JsonObject block = blocks.get(i).getAsJsonObject();
            System.out.println(block.get("text").getAsString());
        }
    }
}

使用 Java 对象映射

更推荐的方式是创建一个对应的 Java 类,然后反序列化:

import com.google.gson.Gson;
import java.util.List;

class Block {
    private String text;
    
    // getters and setters
    public String getText() { return text; }
    public void setText(String text) { this.text = text; }
}

class MyData {
    private int firstLinesChars;
    private int level;
    private List<Block> blocks;
    private String markdownContent;
    private int index;
    private String subType;
    private int lineHeight;
    private String text;
    private String alignment;
    private String type;
    private int pageNum;
    private String uniqueId;
    
    // getters and setters
    public List<Block> getBlocks() { return blocks; }
    public void setBlocks(List<Block> blocks) { this.blocks = blocks; }
    // ... 其他getter/setter
}

public class ObjectMappingExample {
    public static void main(String[] args) {
        String jsonStr = "{\"firstLinesChars\":0,\"level\":0,\"blocks\":[{\"text\":\"电话:123-4567-890\"},{\"text\":\"邮箱:zhangsan@example.com\"}],\"markdownContent\":\"电话:123-4567-890邮箱:zhangsan@example.com  \\n\\n\",\"index\":1,\"subType\":\"none\",\"lineHeight\":12,\"text\":\"电话:123-4567-890邮箱:zhangsan@example.com\\n\",\"alignment\":\"left\",\"type\":\"text\",\"pageNum\":0,\"uniqueId\":\"8412ee4f51eb01881c3a1c722a519c1c\"}";
        
        Gson gson = new Gson();
        MyData data = gson.fromJson(jsonStr, MyData.class);
        
        System.out.println("Unique ID: " + data.getUniqueId());
        System.out.println("Markdown Content: " + data.getMarkdownContent().trim());
        
        System.out.println("\nBlocks:");
        for (Block block : data.getBlocks()) {
            System.out.println(block.getText());
        }
    }
}

选择哪种方法取决于你的具体需求和项目设置。对象映射方式在复杂项目中更易于维护。

posted @ 2025-05-29 23:34  258333  阅读(11)  评论(0)    收藏  举报