我需要帮助阅读.json文件。
我有这个文件(使用CraftStudio的3D动画模型的信息):
{
"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": {
}
}
}
}
我想知道如何在“nodeAnimations”jsonobject中获取所有部分(例如“Upper Arm Right”)。也许一个清单?
问题在于我不知道我想要获得的部件的名称。
对于关键帧(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 ]
}
我如何获得所有信息?它可能有90+
或- keyframe
,而且部分名称可能不同。
使用GSON lib在Java中。另一个json lib会更好吗?
感谢您的帮助:)
答案 0 :(得分:0)
使用gson并解析模型,然后使用键获取值...
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"));
}
您可能需要以某种迭代方式获取值...
然后您可以做的是从json元素获取条目集,然后获取旋转对象
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"));
}
}