嵌套的JSONObject

时间:2010-06-29 09:12:26

标签: java json nested

我遇到了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 =     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 fild,lis Duration,Starttime等等。

当我这样尝试时:

  

String canseekontime =   flash.getString( “Canseekontime”);
    int starttime =   flash.getInt( “开始时间”);
  Double duration = flash.getDouble(“Duration”);

我收到此错误:
net.sf.json.JSONException: JSONObject["Duration"]未找到。

任何帮助??

提前致谢

1 个答案:

答案 0 :(得分:2)

你的意思是

JSONObject flash = json.getJSONObject(0); 
JSONObject Flash = flash.getJSONObject("Flash"); 
int starttime = Flash.getInt("Starttime"); 
相关问题