我在使用 JSONObjects 和 JSONArray 时遇到了一些问题。
我想解析这个 json 文件:
[{
"SourceFile": "AndresIniesta.flv",
"ExifTool": {
"ExifToolVersion": 8.22
},
"System": {
"FileName": "AndresIniesta.flv",
(...)
},
"File": {
"FileType": "FLV",
"MIMEType": "video/x-flv"
},
"Flash": {
"Duration": "04:09",
"Starttime": 0,
"Totalduration": 249.36,
"ImageWidth": 320,
(...)
},
"Composite": {
"ImageSize": "320x240"
}
}]
但不是全部,只是现场Flash。
整个文件是一个 JSONArray,但只有 1 个元素。我得到了充满这段代码的 Flash:
JsonMappingException, IOException {
String a = new String();
InputStream is = this.getClass().getClassLoader().getResourceAsStream( "a.json");
String jsonTxt = IOUtils.toString(is);
JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);
JSONObject flash = json.getJSONObject(0);System.out.print("flash -> " + flash.getString("Flash"));
但我不知道如何访问 Flash 字段、lis Duration、Starttime...等。
当我这样尝试时:
String canseekontime = flash.getString("Canseekontime");
int starttime = flash.getInt("Starttime");
Double duration = flash.getDouble("Duration");
我得到这个错误:
net.sf.json.JSONException: JSONObject["Duration"]
未找到。
有什么帮助吗??
提前致谢
最佳答案
你的意思是
JSONObject flash = json.getJSONObject(0);
JSONObject Flash = flash.getJSONObject("Flash");
int starttime = Flash.getInt("Starttime");
关于java - 嵌套的 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3139293/