Java - JSON解析器错误 [英] Java - JSON Parser Error
问题描述
我正在创建一个应用程序,它将向Web服务器发送 http
请求。返回将在 json
中。这是 json
的样子
[//我用了工具,使其美观,易于阅读。
{
item_name:Adame,
item_type:Special,
item:Chestplate,
item_min_lvl:
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:剑,
item_min_lvl:8,
enchantment:[],// colonm 30 is [
min_dam:9 ,
max_dam:10
}
]
你是c一看,数组里面的数据是不同的。我得到了这个错误,期望BEGIN_OBJECT,但在第1行第30列
处为BEGIN_ARRAY。这是我的代码:
MyJSON [] data = gson.from(jsonString,MyJSON []。class);
class MyJSON {
String item_name;
String item_type;
字符串项目;
字符串item_min_lvl;
附魔附魔;
String min_dam;
字符串max_dam;
@Override
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append(\\\
item_name:)。append(item_name);
builder.append(\\\
item_type:)。append(item_type);
builder.append(\\\
item:)。append(item);
builder.append(\\\
item_min_lvl:)。append(item_min_lvl);
builder.append(\\\
\\\
Enchantment Details:);
builder.append(\\\
health:)。append(enchantment.health);
builder.append(\\\
dam:)。append(enchantment.dam);
builder.append(\\\
life:)。append(enchantment.life);
builder.append(\\\
xp:)。append(enchantment.xp);
builder.append(\\\
loot:)。append(enchantment.loot);
return builder.toString();
}
}
class Enchantment {
字符串健康;
弦乐大坝;
字符串生活;
String xp;
字符串战利品;
字符串法力值;
$ / code>
任何人都可以帮助我改进我的代码,以便我的代码解析 json
在不同情况下。先谢谢了。 (Ps不是我的web服务器,所以我无法对json做任何事情) 这是有效JSON 除非您已添加注释才能显示问题出在哪里?
注释不应该是JSON的一部分。
下面是我已经在另一篇文章中向您分享的代码 ArrayList< Map< String,Object>> > ,因为JSON字符串中的条目不是对称的。在这种情况下,您不能将其转换为POJO。
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File(resources / json2.txt)));
String line = null; ((line = reader.readLine())!= null){
builder.append(line);
while
}
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); (String key,json.keySet()){
System.out.println(key +$)的
$ b $(Map< String,Object> json:list){
:+ json.get(key));
}
System.out.println(===========);
}
输出:
item_name:adame
item_type:Special
item:胸牌
item_min_lvl:50
enchantment:{health = 0.3,dam = 24%, life = 0.1,xp = 24%,loot = 22%}
def:73
===========
item_name:Sticks'
item_type:独特的
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:剑'
item_type:唯一
item:剑
item_min_lvl:8
enchantment:[]
min_dam:9
max_dam:10
===========
编辑
结界返回类似于
enchantment:{health = 0.6,mana = 1,dam = 12% ,生命= 0.3,xp = 17%,loot = 17%}。
我怎样才能得到健康的例子?
类型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));
}
输出:
生命值:0.6
法力:1
dam:12%
人生:0.3
xp:17%
物品:17 %
I am creating an application which it will send http
request to a web server. The return will be in json
. Here is how the json
look like
[//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
. 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. Thanks in advanced. (P.s. that's not my web server so I can't do anything with the json)
This is a Valid JSON unless you have added comments just to show lines where is the issue?
Comments should not be part of JSON.
Here is the code that I have already shared you at you another post Java - Json deserialize data [].
You have to use ArrayList<Map<String, Object>>
because the entries in the JSON string are not symmetric. You can't convert it into POJO in this case.
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%
这篇关于Java - JSON解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!