简体   繁体   English

Java - JSON分析器错误

[英]Java - JSON Parser Error

I am creating an application which it will send http request to a web server. 我正在创建一个应用程序,它将向Web服务器发送http请求。 The return will be in json . 回报将在json Here is how the json look like 以下是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"
  }
]

Are you can see, the data inside the array are different. 你可以看到,阵列内的数据是不同的。 I got this error, Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 30 . 我收到此错误, Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 30 This is my code: 这是我的代码:

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;
}

Can anyone help me to improve my code so my code an parse the json in different case. 任何人都可以帮助我改进我的代码,所以我的代码在不同情况下解析json Thanks in advanced. 提前致谢。 (Ps that's not my web server so I can't do anything with the json) (那不是我的网络服务器,所以我不能用json做任何事情)

Basically this line of JSON 基本上这行JSON

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

doesn't match your POJO. 与您的POJO不符。 You're expecting an Enchantment object, but the JSON is giving you an array. 你期待一个Enchantment对象,但是JSON给你一个数组。 Fix your JSON to return an empty JSON object or nothing at all for the enchantment pair. 修复您的JSON返回一个空的JSON对象或什么都没有了enchantment对。

"enchantment": {}

You can create a custom list type in Gson's fromJson() method to map it to a list of POJOs 您可以在Gson的fromJson()方法中创建自定义列表类型,以将其映射到POJO列表

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

You will get a List<Enhancement> . 您将获得List<Enhancement>

This is a Valid JSON unless you have added comments just to show lines where is the issue? 这是一个有效的JSON,除非您添加注释只是为了显示问题的哪些方面?

Comments should not be part of JSON. 注释不应该是JSON的一部分。

Here is the code that I have already shared you at you another post Java - Json deserialize data [] . 这是我已经在你的另一篇文章中分享给你的代码--Json反序列化数据[]

You have to use ArrayList<Map<String, Object>> because the entries in the JSON string are not symmetric. 您必须使用ArrayList<Map<String, Object>>因为JSON字符串中的条目不对称。 You can't convert it into POJO in this case. 在这种情况下,您无法将其转换为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("===========");
}

output: 输出:

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
===========

EDIT 编辑

enchantment return something like 结界返回类似的东西

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

How can I get for example health? 我怎样才能获得健康?

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));
}

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM