Ze Amateis Ze Amateis - 4 months ago 13
JSON Question

How to get specific information in json with GSON in java

I need some help to read correctly a json file and get all information inside it

I have this file (all informations of a 3D animations for models with CraftStudio):

{
"title": "Idle",
"duration": 120,
"holdLastKeyframe": false,
"nodeAnimations": {
"Upper Arm Right": {
"position": {
},
"offsetFromPivot": {
},
"size": {
},
"rotation": {
"0": [ 0, 0, -4.171811 ],
"30": [ 0, 0, -1.254342 ],
"60": [ 0, 0, -6.620682 ],
"90": [ 0, 0, -2.199501 ]
},
"stretch": {
}
},
"Upper Arm Left": {
"position": {
},
"offsetFromPivot": {
},
"size": {
},
"rotation": {
"0": [ 0, 0, 7.722006 ],
"30": [ 0, 0, 0.891409 ],
"60": [ 0, 0, 7.101573 ],
"90": [ 0, 0, 3.180463 ]
},
"stretch": {
}
},
"Spine": {
"position": {
},
"offsetFromPivot": {
},
"size": {
},
"rotation": {
"0": [ 0, 0, 0 ],
"60": [ 4.786639, 0, 0 ]
},
"stretch": {
}
}
}
}


I want to know how I can get all parts ("Upper Arm Right" by example) in the "nodeAnimations" jsonobject (maybe a list ?)

But I'm assuming that I don't know the names of the parts

and it's the same for the keyframe (0, 30, 60, 90)

"rotation": {
"0": [ 0, 0, -4.171811 ],
"30": [ 0, 0, -1.254342 ],
"60": [ 0, 0, -6.620682 ],
"90": [ 0, 0, -2.199501 ]
}


How I can get all of these informations ? 'cause it could be the have 90+ or - keyframe and the parts may be named differently.

It's in Java with GSON lib'(maybe an other json lib can be better for this work ?)

Thx for your help :)

Answer

Use gson and parse the model, then get the values using the keys...

public static void main(String[] args) {
String input = "{\"title\": \"Idle\", \"duration\": 120, \"holdLastKeyframe\": false, \"nodeAnimations\": {"
    + "\"Upper Arm Right\": { \"position\": { },\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, -4.171811 ],\"30\": [ 0, 0, -1.254342 ],\"60\": [ 0, 0, -6.620682 ],\"90\": [ 0, 0, -2.199501 ]},\"stretch\": {}},\"Upper Arm Left\": {\"position\": {},\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, 7.722006 ],\"30\": [ 0, 0, 0.891409 ],\"60\": [ 0, 0, 7.101573 ],\"90\": [ 0, 0, 3.180463 ]},\"stretch\": {}},\"Spine\": {\"position\": {},\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, 0 ],\"60\": [ 4.786639, 0, 0 ]},\"stretch\": {}}}}";
JsonObject jsonObject = new Gson().fromJson(input, JsonObject.class);
System.out.println(jsonObject);
System.out.println("Upper Arm Right:" + jsonObject.get("nodeAnimations").getAsJsonObject()
    .get("Upper Arm Right").getAsJsonObject().get("rotation"));
System.out.println("Upper Arm Left:" + jsonObject.get("nodeAnimations").getAsJsonObject().get("Upper Arm Left")
    .getAsJsonObject().get("rotation"));
System.out.println("Spine:"
    + jsonObject.get("nodeAnimations").getAsJsonObject().get("Spine").getAsJsonObject().get("rotation"));
}

Edit:

you maybe need the values in some iterative way...

then what you can do is get from the json element the entry set and then get the rotation object

Example:

public static void main(String[] args) {
String input = "{\"title\": \"Idle\", \"duration\": 120, \"holdLastKeyframe\": false, \"nodeAnimations\": {"
    + "\"Upper Arm Right\": { \"position\": { },\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, -4.171811 ],\"30\": [ 0, 0, -1.254342 ],\"60\": [ 0, 0, -6.620682 ],\"90\": [ 0, 0, -2.199501 ]},\"stretch\": {}},\"Upper Arm Left\": {\"position\": {},\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, 7.722006 ],\"30\": [ 0, 0, 0.891409 ],\"60\": [ 0, 0, 7.101573 ],\"90\": [ 0, 0, 3.180463 ]},\"stretch\": {}},\"Spine\": {\"position\": {},\"offsetFromPivot\": {},\"size\": {},\"rotation\": {\"0\": [ 0, 0, 0 ],\"60\": [ 4.786639, 0, 0 ]},\"stretch\": {}}}}";
JsonObject jsonObject = new Gson().fromJson(input, JsonObject.class);
for (Entry<String, JsonElement> entrySet : jsonObject.get("nodeAnimations").getAsJsonObject().entrySet()) {
    System.out.println(entrySet.getKey());
    System.out.println(entrySet.getValue().getAsJsonObject().get("rotation"));
}
}