6

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

2
  • as for me, usually i model the jsons to a pojo -- but you could always get it as a JsonElement and then get as json object, finally get("value") to get value as (sadly) JsonElement.. it's pretty roundabout, but for sure, i'd suggest you to make pojo.. Commented Dec 28, 2016 at 3:30
  • I appreciate your help, but like I said, I'm completely new to this JSON thing and kinda need a little spoon feeding :P Commented Dec 28, 2016 at 3:34

2 Answers 2

6

You can get values using:

JsonObject propertiesJson = properties.get(0);
String value = propertiesJson.getString("value");
Sign up to request clarification or add additional context in comments.

4 Comments

Aha! It works, just not it's not "getString" its "get("value").toString"
@SageM If you are using Json library than try to use its method for better convenience. get value as : propertiesJson.get("value").getAsString();
properties.get(0) is JsonElement, not JsonObject in Gson. I used this code. String name = properties.get(0).getAsJsonObject().get("name").getAsString();
in my case, I had to use jsonObject.get(object1) to get actual object first and then the .toString() method in Gson.fromJson();
0

array is JsonArray object from com.google.gson library

for (int i=0; i<array.size(); i++) {
    JsonObject json = array.get(i).getAsJsonObject();
    String value = json.get("key").getAsString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.