Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested JSONObject

Tags:

java

json

nested

I am having several problems with JSONObjects and JSONArray.
I would like to parse this json file:

[{
  "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"
  }
}]

But not all of them, just the field Flash.
The whole file is an JSONArray, but with just 1 element. I got the Flash filled with this piece of code:

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"));

But I don't know hoy to access to each one of Flash fild, lis Duration, Starttime... etc.

When I try it like this:

String canseekontime = flash.getString("Canseekontime");
int starttime = flash.getInt("Starttime");
Double duration = flash.getDouble("Duration");

I get this error:
net.sf.json.JSONException: JSONObject["Duration"] not found.

Any help??

Thanks in advance

like image 666
Blanca Hdez Avatar asked Sep 22 '26 05:09

Blanca Hdez


1 Answers

You mean

JSONObject flash = json.getJSONObject(0); 
JSONObject Flash = flash.getJSONObject("Flash"); 
int starttime = Flash.getInt("Starttime"); 
like image 65
mplungjan Avatar answered Sep 24 '26 19:09

mplungjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!