【问题标题】:How to read JSON file to Java如何将 JSON 文件读入 Java
【发布时间】:2016-10-17 17:32:48
【问题描述】:

如何编写代码来读取和显示来自web_blockport_blockuser 的键和值?

我的 JSON 文件test.json:

 {
    "web_block":[
            {
                "url" : "www.facebook.com",
                "action" : "deny" 
            },
            {
                "url" : "www.google.com",
                "action" : "deny" 
            },
            {
                "url" : "www.youtube.com",
                "action" : "deny" 
            },
            {
                "url" : "www.wu.ac.th",
                "action" : "allow" 
            }
        ],
    "port_block":[
            {
                "port" : "80",
                "protocol" : "tcp",
                "action" : "block"
            },
            {
                "port" : "443",
                "protocol" : "udp",
                "action" : "allow" 
            }
        ],
       "user": [
                       {
                                "username" : "toms"
                       }
               ]

}

我尝试了以下操作:

JSONParser parser = new JSONParser();
    try {

        Object obj = parser.parse(new FileReader("d:\\test.json"));

        JSONObject jsonObject = (JSONObject) obj;

        String name = (String) jsonObject.get("web_block");
        System.out.println(url);

        long age = (Long) jsonObject.get("port_block");
        System.out.println(port);

但还是错了。

【问题讨论】:

  • 你可以使用Jackson之类的库来解析JSON。
  • 互联网上有几十个关于在 Java 中解析 JSON 的小例子,无论有没有像 Jackson 或 GS​​ON 这样的数据绑定库。 Google 是您的朋友。

标签: java json parsing


【解决方案1】:
  1. 将以下 Jackson 库添加到您的项目中 -

杰克逊数据绑定

杰克逊注解

杰克逊核心

  1. 创建一个映射到您的 json 的 pojo 类 -

    public class CustomJsonData{
    
    @JsonProperty("web_block")
    private List<WebBlock> webBlock = new ArrayList<WebBlock>();
    @JsonProperty("port_block")
    private List<PortBlock> portBlock = new ArrayList<PortBlock>();
    @JsonProperty("user")
    private List<User> user = new ArrayList<User>();
    
    public CustomJsonData() {
    }
    
    public List<WebBlock> getWebBlock() {
        return webBlock;
    }
    
    public void setWebBlock(List<WebBlock> webBlock) {
        this.webBlock = webBlock;
    }
    
    public List<PortBlock> getPortBlock() {
        return portBlock;
    }
    
    public void setPortBlock(List<PortBlock> portBlock) {
        this.portBlock = portBlock;
    }
    
    public List<User> getUser() {
        return user;
    }
    
    public void setUser(List<User> user) {
        this.user = user;
    }
    
    }
    
    class PortBlock {
    
    @JsonProperty("port")
    private String port;
    @JsonProperty("protocol")
    private String protocol;
    @JsonProperty("action")
    private String action;
    
    public PortBlock() {
    }
    
    public String getPort() {
        return port;
    }
    
    public void setPort(String port) {
        this.port = port;
    }
    
    public String getProtocol() {
        return protocol;
    }
    
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    
    public String getAction() {
        return action;
    }
    
    public void setAction(String action) {
        this.action = action;
    }
    
    }
    
    class User {
    
    @JsonProperty("username")
    private String username;
    
    public User() {
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    }
    
    class WebBlock {
    
    @JsonProperty("url")
    private String url;
    @JsonProperty("action")
    private String action;
    
    public WebBlock() {
    }
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public String getAction() {
        return action;
    }
    
    public void setAction(String action) {
        this.action = action;
    }
    
    }
    

说明:“[ ]”下的json中的数据是一个数组,所以它是用数组列表映射的,而“{}”下的数据是一个对象,所以它是映射的具有字符串类型字段的特定类(例如:WebBlock、PortBlock 等)。

现在您可以检索如下数据 -

ObjectMapper mapper = new ObjectMapper();
CustomJsonData object = mapper.readValue(new FileReader("d:\\test.json"),CustomJsonData.class);     
String username = object.getUser().get(0).getUsername();
List<WebBlock> webBlocks = object.getWebBlock();
List<PortBlock> portBlocks = object.getPortBlock();

请参阅此处的示例 - http://wiki.fasterxml.com/JacksonInFiveMinutes

【讨论】:

  • 在 ObjectMapper 中映射器 = new ObjectMapper(); CustomJsonData 对象 = mapper.readValue(new FileReader("d:\\test.json"),CustomJsonData.class);字符串用户名 = object.getUser().get(0).getUsername(); List webBlocks = object.getWebBlock(); List portBlocks = object.getPortBlock();
  • 在对象中必须放在任何类中。
  • 对不起,我没有得到你的要求。
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2017-01-18
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2012-03-09
  • 2015-02-26
相关资源
最近更新 更多