Java - JSON分析器错误

时间:2014-05-18 05:06:10

标签: java json gson

我正在创建一个应用程序,它将向网络服务器发送http请求。返回将在json。以下是json的外观

[//I used a tool to make it beautiful and easy to read.
  {
    "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"
  },
  {
    "item_name": "Sword'",
    "item_type": "Unique",
    "item": "Sword",
    "item_min_lvl": "8",
    "enchantment": [], //colonm 30 is [
    "min_dam": "9",
    "max_dam": "10"
  }
]

你可以看到,阵列内的数据是不同的。我收到了这个错误Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 30。这是我的代码:

MyJSON[] data = gson.from(jsonString, MyJSON[].class);

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

    @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;
    String mana;
}

任何人都可以帮助我改进我的代码,以便我的代码在不同情况下解析json。提前致谢。 (那不是我的网络服务器所以我不能对json做任何事情)

3 个答案:

答案 0 :(得分:2)

基本上这行JSON

"enchantment": [], //colonm 30 is [

与您的POJO不符。你期待一个Enchantment对象,但JSON给你一个数组。修复您的JSON以返回空JSON对象,或者根本不为enchantment对返回任何内容。

"enchantment": {}

答案 1 :(得分:0)

您可以在Gson的fromJson()方法中创建自定义列表类型,以将其映射到POJO列表

Type listType = new TypeToken<ArrayList<Enhancement>>() {}.getType();
List<Enhancement> enhancements = new Gson().fromJson(jsonString, listType);

您将获得List<Enhancement>

答案 2 :(得分:0)

这是有效JSON ,除非您添加评论只是为了显示问题在哪里?

评论不应该是JSON的一部分。

以下是我已经在您的另一篇文章Java - Json deserialize data []上与您分享的代码。

您必须使用ArrayList<Map<String, Object>>,因为JSON字符串中的条目不对称。在这种情况下,您无法将其转换为POJO。

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

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
===========
item_name:Sword'
item_type:Unique
item:Sword
item_min_lvl:8
enchantment:[]
min_dam:9
max_dam:10
===========

修改

结界返回类似

的东西
enchantment:{health=0.6, mana=1, dam=12%, life=0.3, xp=17%, loot=17%}. 

我怎样才能获得健康?

Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
String string = "{health=0.6, mana=1, dam=12%, life=0.3, xp=17%, loot=17%}";
Map<String, String> map = new Gson().fromJson(string, mapType);
for (String key : map.keySet()) {
    System.out.println(key + ":" + map.get(key));
}

输出:

health:0.6
mana:1
dam:12%
life:0.3
xp:17%
loot:17%