I have this code:
try {
URL url = new URL("My api url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", "MyUserAgent");
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
JsonElement element = new JsonParser().parse(reader);
System.out.println(element);
} catch (IOException e) {
e.printStackTrace();
}
My api response:
[
{
"id": 12345,
"name": "1.0"
}
]
I need to get the name
parameter as a string, but I don't know how to do it.
I think you are looking for :
JsonElement name = element.getAsJsonArray().get(0).getAsJsonObject().get("name");
Output
"1.0"
Or to get the value of name :
String name = element.getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString();
Output
1.0
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.