gpt4 book ai didi

java - 使用 Gson 反序列化 json 不起作用

转载 作者:行者123 更新时间:2023-12-01 13:11:59 24 4
gpt4 key购买 nike

类包含所有歌曲

public class Songs{
private List levels;

public List getLevels() {
return levels;
}

public void setLevels(List levels) {
this.levels = levels;
}
}

每首歌曲对象

public class Levels{
private Number id;
private String name;
private List sequence;

public Number getId(){
return this.id;
}
public void setId(Number id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getSequence() {
return sequence;
}
public void setSequence(List sequence) {
this.sequence = sequence;
}
}

JSON

{
"levels": [
{
"id": 1,
"name": "Sequence",
"sequence": [
17,
1,
2
]
},
{
"id": 2,
"name": "Sequence",
"sequence": [
17,
0,
1,
2,
4,
4,
5,
6
]
}
]
}

Java代码

如果我调试,我可以看到对象,但问题是获取 int 数组的序列,这是可行的。谁能帮我 ???如果我这样做,我可以粘贴堆栈跟踪 list2.getLevels().get(0).getSequence();

String json = new String(b);
Gson gson = new Gson();
Songs list2 = (Songs) gson.fromJson(json, Songs.class);

//I CAN READ ONE LEVEL LIKE THIS
LinkedTreeMap<String,Levels> l = (LinkedTreeMap)songs.getLevels().get(0);


//HOW CAN I GET SEQUENCE ARRAY???

最佳答案

关卡.java

import java.util.List;


public class Levels {
private Number id;
private String name;
private List sequence;

public Number getId(){
return this.id;
}
public void setId(Number id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getSequence() {
return sequence;
}
public void setSequence(List sequence) {
this.sequence = sequence;
}
}

歌曲.java

import java.util.List;

public class Songs{
private List<Levels> levels;

public List<Levels> getLevels() {
return levels;
}

public void setLevels(List<Levels> levels) {
this.levels = levels;
}

}

并使用它来获取序列列表:

String json = new String("{\n    \"levels\": [\n        {\n            \"id\": 1,\n            \"name\": \"Sequence\",\n            \"sequence\": [\n                17,\n                1,\n                2\n            ]\n        },\n        {\n            \"id\": 2,\n            \"name\": \"Sequence\",\n            \"sequence\": [\n                17,\n                0,\n                1,\n                2,\n                4,\n                4,\n                5,\n                6\n            ]\n        }\n    ]\n}");

Gson g = new Gson();
Songs vc = (Songs)g.fromJson(json, Songs.class);
List test = vc.getLevels().get(0).getSequence();

关于java - 使用 Gson 反序列化 json 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com