Java - Json反序列化数据[] [英] Java - Json deserialize data []
问题描述
我是新来的stackoverflow。
我正在创建一个Java应用程序,它将从Web服务器获取数据。数据是json格式。示例
[
{
item_name:Adame,
item_type :特殊,
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,正如你看到的,它是以<$ c $开头的c> [。我从来没有反序列化过这种情况,而且json数据也不一样(例如 enchantment
)。在谷歌搜索,但我找不到任何类似的情况。任何人都可以帮助我的代码?感谢高级。
请试用此代码。你会得到你的问题的答案。它是一个包含2项的列表。
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File(resources / json1.txt)));
String line = null; ((line = reader.readLine())!= null){
builder.append(line);
while
}
reader.close();
Gson gson = new Gson();
Type typeType = new TypeToken< ArrayList< MyJSON>>(){
} .getType();
列表< MyJSON> list = gson.fromJson(builder.toString(),listType);
//你也可以试试这个表单
// MyJSON [] list = gson.fromJson(builder.toString(),MyJSON []。class); (MyJSON json:list){
System.out.println(json.toString());
}
...
class MyJSON {
String item_name;
String item_type;
字符串项目;
字符串item_min_lvl;
附魔附魔;
@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;
字符串战利品;
}
输出:
item_name:adame
item_type:Special
item:胸牌
item_min_lvl:50
附魔详情:
健康:0.3
dam:24%
人生:0.1
xp:24%
战利品:22%
$ b item_name: b $ b item_type:Unique
item:Stick
item_min_lvl:4
附魔详情:
health:0.6
dam:12%
life:0.3
xp:17%
loot:17%
编辑
每个条目的结构都不相同,因此您不能使用此类型的POJO使用 ArrayList< Map< / code>来访问基于键的值从地图。
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
===========
I am new to stackoverflow.
I am creating an Java application which it will get data from a web server. The data is in json format. Example"
[
{
"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"
}
]
I know how to deserialize json using Gson. As you can see, it's started with [
. I never deserialize this case before. Also, the json data is not the same(e.g. enchantment
). I also searched in Google but I can't find any similar case. Can anyone help me with the code? Thanks in advanced.
Try with this code. You will get the answer of your question. It's an List with 2 items.
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;
}
output:
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%
EDIT
The structure of each entry is not same hence you can't use POJO for this type of JSON.
Simply use ArrayList<Map<String, Object>>
and access the value based on key from the map.
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
===========
这篇关于Java - Json反序列化数据[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!