将 Json 解析为 Java bean
我正在尝试解析这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这个元数据中包含所有信息
In this meta is all the information
该问题混合了
com.fasterxml.jackson
和net.sf.json
可互换库。@Blanca 给出了杰克逊的答案。这是
net.sf.json
替代方案:NoSuchMethodException: Unknown property 'ExifTool'
被抛出,因为 PropertySetStrategy.DEFAULT 需要公共字段或设置器。The question mixes
com.fasterxml.jackson
andnet.sf.json
interchangeable libraries.@Blanca gave answer for jackson. And here is the
net.sf.json
alternative:The
NoSuchMethodException: Unknown property 'ExifTool'
was thrown because PropertySetStrategy.DEFAULT requires public fields or setters, I guess.