gson Java - Json反序列化数据[]

oogrdqng  于 2023-01-13  发布在  Java
关注(0)|答案(2)|浏览(178)

我是stackoverflow的新手。
我正在创建一个Java应用程序,它将从Web服务器获取数据。数据是json格式。示例”

[
  {
    "item_name": "Adame",
    "item_type": "Special",
    "item": "Chestplate",
    "item_min_lvl": "50",
    "enchantment": {
      "health": "0.3",
      "dam": "24%",
      "life": "0.1",
      "xp": "24%",
      "loot": "22%"
    },
    "def": "73"
  },
  {
    "item_name": "Sticks'",
    "item_type": "Unique",
    "item": "Stick",
    "item_min_lvl": "4",
    "enchantment": {
      "health": "0.6",
      "mana": "1",
      "dam": "12%",
      "life": "0.3",
      "xp": "17%",
      "loot": "17%"
    },
    "min_dam": "39",
    "max_dam": "34"
  }
]

我知道如何使用Gson反序列化json。正如你所看到的,它是以[开头的。我以前从未反序列化过这种情况。另外,json数据也不一样(例如enchantment)。我也在谷歌上搜索过,但我找不到任何类似的情况。有人能帮我写代码吗?

kyxcudwk

kyxcudwk1#

尝试与此代码。你会得到你的问题的答案。这是一个列表有2个项目。

StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json1.txt")));
String line = null;
while ((line = reader.readLine()) != null) {
    builder.append(line);
}
reader.close();

Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<MyJSON>>() {
}.getType();
List<MyJSON> list = gson.fromJson(builder.toString(), listType);
// you can try this form as well
// MyJSON[] list = gson.fromJson(builder.toString(), MyJSON[].class);

for (MyJSON json : list) {
    System.out.println(json.toString());
}

...

class MyJSON {
    String item_name;
    String item_type;
    String item;
    String item_min_lvl;
    Enchantment enchantment;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();

        builder.append("\nitem_name:").append(item_name);
        builder.append("\nitem_type:").append(item_type);
        builder.append("\nitem:").append(item);
        builder.append("\nitem_min_lvl:").append(item_min_lvl);

        builder.append("\n\nEnchantment Details:");
        builder.append("\nhealth:").append(enchantment.health);
        builder.append("\ndam:").append(enchantment.dam);
        builder.append("\nlife:").append(enchantment.life);
        builder.append("\nxp:").append(enchantment.xp);
        builder.append("\nloot:").append(enchantment.loot);
        return builder.toString();
    }
}

class Enchantment {
    String health;
    String dam;
    String life;
    String xp;
    String loot;
}

输出:

item_name:Adame
item_type:Special
item:Chestplate
item_min_lvl:50

Enchantment Details:
health:0.3
dam:24%
life:0.1
xp:24%
loot:22%

item_name:Sticks'
item_type:Unique
item:Stick
item_min_lvl:4

Enchantment Details:
health:0.6
dam:12%
life:0.3
xp:17%
loot:17%
    • 编辑**

每个条目的结构都不相同,因此不能将POJO用于这种类型的JSON。
只需使用ArrayList<Map<String, Object>>并从Map中访问基于key的值。

Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<Map<String, Object>>>() {
}.getType();
ArrayList<Map<String, Object>> list = gson.fromJson(builder.toString(), listType);

for (Map<String, Object> json : list) {
    for (String key : json.keySet()) {
        System.out.println(key + ":" + json.get(key));
    }
    System.out.println("===========");
}

输出:

item_name:Adame
item_type:Special
item:Chestplate
item_min_lvl:50
enchantment:{health=0.3, dam=24%, life=0.1, xp=24%, loot=22%}
def:73
===========
item_name:Sticks'
item_type:Unique
item:Stick
item_min_lvl:4
enchantment:{health=0.6, mana=1, dam=12%, life=0.3, xp=17%, loot=17%}
min_dam:39
max_dam:34
===========
mspsb9vt

mspsb9vt2#

这在Java和GSON中实际上是有效的:

YourObject[] locs = gson.fromJson (someJsonString, YourObject[].class);

它将解析并返回一个YourObject数组,只需创建表示JSON对象的Java类,并根据需要替换占位符。
编辑:正如Braj之前所说的,您可以创建一个完全形式的POJO,包括其他(非对称)属性(我在这里借用了Braj回答中的代码):

//... snip ...
class MyJSON
{
    String      item_name;
    String      item_type;
    String      item;
    String      item_min_lvl;
    Enchantment enchantment;

    // Heres the other attributes
    String      min_dam;
    String      max_dam;
}
//... snip ...

如果原始JSON中没有提供这些值,GSON将解析它并将其设置为null
然而,从另一个问题来看,似乎为附魔提供的JSON(Java - JSON Parser Error)不一致,所以这会导致问题。我建议将附魔的JSON作为一个数组发送以保持一致性,然后您可以将POJO结构为:

//... snip ...
class MyJSON
{
    String      item_name;
    String      item_type;
    String      item;
    String      item_min_lvl;
    Enchantment[]   enchantment;

    // Heres the other attributes
    String      min_dam;
    String      max_dam;
}
//... snip ...

相关问题