如何使用gson从JSON过滤数据
我有这个JSON文件:
{"status":"success","data":[{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},{"id":2,"employee_name":"Garrett Winters","employee_salary":170750,"employee_age":63,"profile_image":""},{"id":3,"employee_name":"Ashton Cox","employee_salary":86000,"employee_age":66,"profile_image":""}
为了读取“状态”,我的代码看起来像这样,正常工作:
JsonObject json = new Gson().fromJson(responseText, JsonObject.class);
System.out.println(json.get("status"));
问题:我如何获得第二个员工_name(Garrett Winters)? 我已经尝试了这些,但是它不起作用 :-(
System.out.println(json.get("data.employee_name(2)"));
System.out.println(json.get("data[2].employee_name"));
System.out.println(json.get("data/employee_name(2)"));
System.out.println(json.get("data/employee_name[2]"));
I have this JSON file:
{"status":"success","data":[{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},{"id":2,"employee_name":"Garrett Winters","employee_salary":170750,"employee_age":63,"profile_image":""},{"id":3,"employee_name":"Ashton Cox","employee_salary":86000,"employee_age":66,"profile_image":""}
For reading the "status" my code looks like this and it WORKS FINE:
JsonObject json = new Gson().fromJson(responseText, JsonObject.class);
System.out.println(json.get("status"));
Question: How can I get e.g. the second employee_name (Garrett Winters)?
I have tried out these, but it DOES NOT WORK :-(
System.out.println(json.get("data.employee_name(2)"));
System.out.println(json.get("data[2].employee_name"));
System.out.println(json.get("data/employee_name(2)"));
System.out.println(json.get("data/employee_name[2]"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要链方法才能获得所需的东西。例如
You need to chain methods in order to get what you want. For example