将 Json 解析为 Java bean

发布于 2024-09-15 21:53:23 字数 1882 浏览 11 评论 0原文

我正在尝试解析这样的 Json 文件,由 Exiftool 生成:

[{
  "SourceFile": "videos/XaviHernandez.flv",
  "ExifTool": {
    "ExifToolVersion": 8.22
  },
  "System": {
    "FileName": "XaviHernandez.flv",
    "Directory": "videos",
    "FileSize": "16 MB",
    "FileModifyDate": "2010:06:17 09:57:21+02:00",
    "FilePermissions": "rw-r--r--"
  },
  "File": {
    "FileType": "FLV",
    "MIMEType": "video/x-flv"
  }
}]

在具有此结构的 Java bean 中:

public class MetadataContentBean {
    ExifToolBean exiftoolBean;
    String SourceFile;
    FileBean fileBean;
    SystemBean systemBean;
//Getters and setter
}

我的 java 代码是这样的:

    InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
    String jsonTxt = IOUtils.toString(is);
    JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
    JSONObject metadatacontent = json.getJSONObject(0);
    ObjectMapper mapper = new ObjectMapper();
    MetadataContentBean meta = new MetadataContentBean();
    mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);
    meta= (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

但我收到此错误:

net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'ExifTool'
    at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45)
    at net.sf.json.JSONObject.setProperty(JSONObject.java:1477)
    at net.sf.json.JSONObject.toBean(JSONObject.java:468)
    at net.sf.json.JSONObject.toBean(JSONObject.java:253)
    at com.playence.parser.JSon.Parser(JSon.java:66)
    at com.playence.parser.JSon.main(JSon.java:28)
Caused by: java.lang.NoSuchMethodException: Unknown property 'ExifTool'

我已经检查了几个论坛,但解决方案是这样的,所以我不知道为什么我没有得到结果。

有什么想法吗?

I am trying to parse a Json file like this, generated by Exiftool:

[{
  "SourceFile": "videos/XaviHernandez.flv",
  "ExifTool": {
    "ExifToolVersion": 8.22
  },
  "System": {
    "FileName": "XaviHernandez.flv",
    "Directory": "videos",
    "FileSize": "16 MB",
    "FileModifyDate": "2010:06:17 09:57:21+02:00",
    "FilePermissions": "rw-r--r--"
  },
  "File": {
    "FileType": "FLV",
    "MIMEType": "video/x-flv"
  }
}]

In a Java bean with this structure:

public class MetadataContentBean {
    ExifToolBean exiftoolBean;
    String SourceFile;
    FileBean fileBean;
    SystemBean systemBean;
//Getters and setter
}

My java code is this:

    InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
    String jsonTxt = IOUtils.toString(is);
    JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
    JSONObject metadatacontent = json.getJSONObject(0);
    ObjectMapper mapper = new ObjectMapper();
    MetadataContentBean meta = new MetadataContentBean();
    mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);
    meta= (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

But I get this error:

net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'ExifTool'
    at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45)
    at net.sf.json.JSONObject.setProperty(JSONObject.java:1477)
    at net.sf.json.JSONObject.toBean(JSONObject.java:468)
    at net.sf.json.JSONObject.toBean(JSONObject.java:253)
    at com.playence.parser.JSon.Parser(JSon.java:66)
    at com.playence.parser.JSon.main(JSon.java:28)
Caused by: java.lang.NoSuchMethodException: Unknown property 'ExifTool'

I have checked in several forums, but the solution is this, so I don't know why I don't get results.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-09-22 21:53:24
ObjectMapper mapper = new ObjectMapper();
MetadataContentBean meta= mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);

在这个元数据中包含所有信息

ObjectMapper mapper = new ObjectMapper();
MetadataContentBean meta= mapper.readValue(metadatacontent.toString(), MetadataContentBean.class);

In this meta is all the information

莫相离 2024-09-22 21:53:24

该问题混合了 com.fasterxml.jacksonnet.sf.json 可互换库。

@Blanca 给出了杰克逊的答案。这是 net.sf.json 替代方案:

JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
JSONObject metadatacontent = json.getJSONObject(0);
MetadataContentBean meta = (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

NoSuchMethodException: Unknown property 'ExifTool' 被抛出,因为 PropertySetStrategy.DEFAULT 需要公共字段或设置器。

The question mixes com.fasterxml.jackson and net.sf.json interchangeable libraries.

@Blanca gave answer for jackson. And here is the net.sf.json alternative:

JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
JSONObject metadatacontent = json.getJSONObject(0);
MetadataContentBean meta = (MetadataContentBean) JSONObject.toBean(metadatacontent, MetadataContentBean.class);

The NoSuchMethodException: Unknown property 'ExifTool' was thrown because PropertySetStrategy.DEFAULT requires public fields or setters, I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文