使用gson从JsonArray获取值

8fq7wneg  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(200)

我已经到处搜索,但找不到如何做到这一点,我超级卡住了。我没有经验与JSON文件,所以勺子喂养是赞赏沿着解释。
我在这里有这个JSON文本用于测试:

{
      "id":"4566e69fc90748ee8d71d7ba5aa00d20",
      "properties":
                    [
                     {
                      "name":"textures",
                      "value":"eyJ0aW1lc3RhbXAiOjE0ODI4ODAxNDMwNzYsInByb2ZpbGVJZCI6IjQ1NjZlNjlmYzkwNzQ4ZWU4ZDcxZDdiYTVhYTAwZDIwIiwicHJvZmlsZU5hbWUiOiJUaGlua29mZGVhdGgiLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTNlODFiOWUxOWFiMWVmMTdhOTBjMGFhNGUxMDg1ZmMxM2NkNDdjZWQ1YTdhMWE0OTI4MDNiMzU2MWU0YTE1YiJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjJiOWM1ZWE3NjNjODZmYzVjYWVhMzNkODJiMGZhNjVhN2MyMjhmZDMyMWJhNTQ3NjZlYTk1YTNkMGI5NzkzIn19fQ==",
                     },
                    ],
      "name":"Thinkofdeath",
    }

我目前拥有:

JsonElement playerProfile = new JsonParser().parse(jsonLine);
    JsonObject jsonProfile = playerProfile.getAsJsonObject();
    JsonArray properties = jsonProfile.getAsJsonArray("properties");

返回[

[
  {
    "name":"textures",
    "value":"eyJ0aW1lc3RhbXAiOjE0ODI4ODAxNDMwNzYsInByb2ZpbGVJZCI6IjQ1NjZlNjlmYzkwNzQ4ZWU4ZDcxZDdiYTVhYTAwZDIwIiwicHJvZmlsZU5hbWUiOiJUaGlua29mZGVhdGgiLCJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTNlODFiOWUxOWFiMWVmMTdhOTBjMGFhNGUxMDg1ZmMxM2NkNDdjZWQ1YTdhMWE0OTI4MDNiMzU2MWU0YTE1YiJ9LCJDQVBFIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMjJiOWM1ZWE3NjNjODZmYzVjYWVhMzNkODJiMGZhNjVhN2MyMjhmZDMyMWJhNTQ3NjZlYTk1YTNkMGI5NzkzIn19fQ==",
  },
]

当然可以。我如何从这个JsonArray中获得“值”呢?注意我使用的是Google的API,Gson

3npbholx

3npbholx1#

您可以使用以下方法获得values

JsonObject propertiesJson = properties.get(0);
String value = propertiesJson.getString("value");
yzuktlbb

yzuktlbb2#

数组是来自com.google.gson库的JsonArray对象

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

相关问题