[英]Need to get id value from json array in java
JSONObject test = get_details();
Here is my input: 这是我的输入:
test = {"users": [{"id": 1, "address": "CA"}]}
System.out.println("test : " + test);
users = test.get("users")
System.out.println("users : " + users);
output: 输出:
test : {"users": [{"id": 1, "address": "CA"}]}
users : [{"id": 1, "address": "CA"}]
I want to get the id
value from users as below: 我想从用户那里获取id
值,如下所示:
1
How can I get it? 我怎么才能得到它?
Users is an array. 用户是一个数组。 So you can try: 因此,您可以尝试:
users[0].id
You can use JSONObject and parse your JSON String like this: 您可以使用JSONObject并按如下方式解析JSON字符串:
String jsonMessage='{"users": [{"id": 1, "address": "CA"}]}';
Scanner scanner = new Scanner(jsonMessage);
while (scanner.hasNextLine()) {
JsonObject jsonObject = Json.createReader(new StringReader(scanner.nextLine())).readObject();
System.out.println("the id is :"+jsonObject.getInt("id"));
}
And you can manage the jsonObject.getInt("id")
like you want. 而且,您可以根据需要管理jsonObject.getInt("id")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.