【问题标题】:LibGDX parsing standard json to an objectLibGDX 将标准 json 解析为对象
【发布时间】:2015-07-20 13:51:00
【问题描述】:

我有一个非常基本的问题。 我阅读了几次关于 JSON 和 Google 的 LibGDX 文档以寻求答案,但它仍然不起作用..

基本上我是从这样的服务器中提取 json:

{"id":1,"facebook_id":"23432232","json":"{\"json\":\"test\"}"}

我有这样的课:

public class ServerJson
{
public static final String NAME = "ServerJson";

private int id;
private String facebookID;
private String json;

public ServerJson(){}

public ServerJson(int id, String facebookID, String json)
{
    this.id = id;
    this.facebookID = facebookID;
    this.json = json;
}

public int getId() {
    return id;
}

public String getFacebookID() {
    return facebookID;
}

public String getJson() {
    return json;
}

当我尝试解析代码时,它不起作用。我得到空值:

String resultString = httpResponse.getResultAsString(); //{"id":1,"facebook_id":"23432232","json":"{\"json\":\"test\"}"}
Json json = new Json();
ServerJson serverJson = json.fromJson(ServerJson.class, resultString);
log(serverJson.getFacebookID()); //<< Is null.

【问题讨论】:

  • resultString 是什么样子的
  • 更新了,和上面的结果一样。
  • 你能做 serverJson.getId() 吗?
  • 谢谢伙计,我整天都在努力解决这个问题。是不是字段不匹配那么简单!!!

标签: java json libgdx


【解决方案1】:

确保您的对象类的字段与 json 对象的字段匹配。

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多