Getting a Value from a JsonArray using gson
10,639
You can get values
using:
JsonObject propertiesJson = properties.get(0);
String value = propertiesJson.getString("value");
Author by
Sage M
Updated on June 19, 2022Comments
-
Sage M almost 2 years
I have searched everywhere and cannot find out how to do this, I'm super stuck. I have NO experience with JSON files, so spoon feeding is appreciated along with an explanation.
I have this JSON text here for testing:
{ "id":"4566e69fc90748ee8d71d7ba5aa00d20", "properties": [ { "name":"textures", "value":"eyJ0aW1lc3RhbXAiOjE0ODI4ODAxNDMwNzYsInByb2ZpbGVJZCI6IjQ1NjZlNjlmYzkwNzQ4ZWU4ZDcxZDdiYTVhYTAwZDIwIiwicHJvZmlsZU5hbWUiOiJUaGlua29mZGVhdGgiLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTNlODFiOWUxOWFiMWVmMTdhOTBjMGFhNGUxMDg1ZmMxM2NkNDdjZWQ1YTdhMWE0OTI4MDNiMzU2MWU0YTE1YiJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjJiOWM1ZWE3NjNjODZmYzVjYWVhMzNkODJiMGZhNjVhN2MyMjhmZDMyMWJhNTQ3NjZlYTk1YTNkMGI5NzkzIn19fQ==", }, ], "name":"Thinkofdeath", }
I currently have this:
JsonElement playerProfile = new JsonParser().parse(jsonLine); JsonObject jsonProfile = playerProfile.getAsJsonObject(); JsonArray properties = jsonProfile.getAsJsonArray("properties");
Which returns [
[ { "name":"textures", "value":"eyJ0aW1lc3RhbXAiOjE0ODI4ODAxNDMwNzYsInByb2ZpbGVJZCI6IjQ1NjZlNjlmYzkwNzQ4ZWU4ZDcxZDdiYTVhYTAwZDIwIiwicHJvZmlsZU5hbWUiOiJUaGlua29mZGVhdGgiLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTNlODFiOWUxOWFiMWVmMTdhOTBjMGFhNGUxMDg1ZmMxM2NkNDdjZWQ1YTdhMWE0OTI4MDNiMzU2MWU0YTE1YiJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjJiOWM1ZWE3NjNjODZmYzVjYWVhMzNkODJiMGZhNjVhN2MyMjhmZDMyMWJhNTQ3NjZlYTk1YTNkMGI5NzkzIn19fQ==", }, ]
Of course. How do I get the "value" from this JsonArray? Note I'm using Google's API, Gson
-
Sage M over 7 yearsAha! It works, just not it's not "getString" its "get("value").toString"
-
Darshit over 7 years@SageM If you are using Json library than try to use its method for better convenience. get value as : propertiesJson.get("value").getAsString();
-
eyeballs almost 6 yearsproperties.get(0) is JsonElement, not JsonObject in Gson. I used this code. String name = properties.get(0).getAsJsonObject().get("name").getAsString();