gpt4 book ai didi

java - InputStream 到 JsonObject - GSON

转载 作者:IT老高 更新时间:2023-10-28 23:35:47 29 4
gpt4 key购买 nike

我在将结果从这个 api 转换为 JsonObject 时遇到了很多麻烦。

无论我尝试做什么,“结果”都保持为空。

  URL url = new URL(urlString);
InputStream input = url.openStream();
Reader reader = new InputStreamReader(in, "UTF-8");
JsonResult result = new Gson().fromJson(reader, JsonResult.class);

JsonResult 类

public class JsonResult {
private String status;
private Meta meta;
private ArrayList<Player> players;
}

class Meta{
private String count;
}

json:

{"status":"ok","meta":{"count":12},"data":[{"nickname":"DataBase","account_id":500566109},{"nickname":"database007","account_id":514382449},{"nickname":"Database04","account_id":504367425},{"nickname":"database08","account_id":515081772},{"nickname":"database1","account_id":503282284},{"nickname":"database1221","account_id":506709044},{"nickname":"database123","account_id":508409172},{"nickname":"database1337","account_id":501661259},{"nickname":"database169","account_id":503884400},{"nickname":"database2","account_id":504337382},{"nickname":"database93","account_id":518691821},{"nickname":"databaseking66","account_id":505911069}]}

注意:这是一个学校项目

注意 2:我确实检查并测试了 SO 的许多其他解决方案,但没有找到或理解正确的解决方案。

编辑 1:

public class JsonResult {
@SerializedName("status")
public String status;
@SerializedName("meta")
public Meta meta;
@SerializedName("data")
public Player[] players;
}

class Meta{
@SerializedName("count")
private String count;
}

播放器类

public class Player {

private int account_id;
private String nickname;

public Player(int account_id, String nickname){
this.account_id = account_id;
this.nickname = nickname;
}


//Generated
public void setAccount_id(int account_id) {
this.account_id = account_id;
}

//Generated
public void setNickname(String nickname) {
this.nickname = nickname;
}

public String toString() {
return this.account_id + this.nickname;
}
}

最佳答案

在你的 JsonResult 类改变

public class JsonResult {
private String status;
private Meta meta;
@SerializedName("data")
private ArrayList<Player> players;
}

关于java - InputStream 到 JsonObject - GSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085721/

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