I have a code where i get a Json response in array format
{ "data": {
"findAllUsers": {
"pendingUsers": [
{
"userId": "123",
"firstName": "FirstNamemz8",
"lastName": "LastName0Hz",
"status": "Pending"
},
{
"userId": "456",
"firstName": "FirstNameEgl",
"lastName": "LastNameVCC",
"status": "Pending"
}
]
} }}
I need to get the list of userID in list format. below is the code i used which doesnt works. Kindly help me out in this with working code.
package com.deedsing;
import com.deedsing.bean.SnowIncident;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Test {
public static void main(String[] args) {
String jString="{ \"data\": {\r\n" +
"\"findAllUsers\": {\r\n" +
" \"pendingUsers\": [\r\n" +
" {\r\n" +
" \"userId\": \"123\",\r\n" +
" \"firstName\": \"FirstNamemz8\",\r\n" +
" \"lastName\": \"LastName0Hz\",\r\n" +
" \"status\": \"Pending\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"userId\": \"456\",\r\n" +
" \"firstName\": \"FirstNameEgl\",\r\n" +
" \"lastName\": \"LastNameVCC\",\r\n" +
" \"status\": \"Pending\"\r\n" +
" }\r\n" +
" ]\r\n" +
"} }}";
Gson json = new GsonBuilder().create();
JsonElement elem = new JsonParser().parse(jString);
JsonObject obj = elem.getAsJsonObject();
JsonArray jarray = obj.get("data").getAsJsonObject().get("findAllUsers").getAsJsonObject().get("pendingUsers").getAsJsonArray();
for(int i = 0; i < jarray.size(); i++)
{
JsonElement elem2 = jarray.get(i);
JsonObject obj2 = elem2.getAsJsonObject();
System.out.println("UserID -->" + obj2.get("userId").getAsString());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.