【问题标题】:Objects in JSON array. Store each of them to ArrayListJSON 数组中的对象。将它们中的每一个存储到 ArrayList
【发布时间】:2015-05-26 13:17:38
【问题描述】:

所以我有这种 JSON。(通过 ASP.NET Web 服务检索)。

    d: [
{
    __type: webservice+Info
    name: Bobo
    score: 55
}
{
    __type: webservice+Info
    name: Mickey
    score: 26
}
{
    __type: webservice+Info
    name: KxKnight
    score: 85
}
]

我已经有一个类将拥有它自己的特定值(名称和分数)。现在我只想将它们保存到 ArrayList。我正在使用 LIBGDX JSON 库。

任何帮助appriciated。

编辑:

我试过了

ArrayList<TopScore> list1 = json.fromJson(ArrayList.class, TopScore.class, json);

但我在上面得到了 NPE。

java.lang.NullPointerException
    at com.badlogic.gdx.utils.Json.readValue(Json.java:912)
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:720)
    at com.mygdx.game.Networking2$1.handleHttpResponse(Networking2.java:161)
    at com.badlogic.gdx.net.NetJavaImpl$1.run(NetJavaImpl.java:198)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

【问题讨论】:

  • 到目前为止你有什么代码,它有什么问题?
  • 嘿。请查看我编辑的问题。
  • 在这之前你是在初始化list1吗?
  • 否,因为“fromJson”方法使用反射并返回 ArrayList 的完整实例。 NPE 来自“fromJson”方法而不是“list1”实例。
  • 它是否适用于使用像 {"example":"value"} 这样的双引号的 json? json 解析器可能对此很严格。另外不要忘记 libgdx 是开源的,因此您可以实际调试该堆栈跟踪并查看它失败的位置。

标签: java json libgdx


【解决方案1】:

您传入的 json 字符串为空。

如果您跟踪堆栈跟踪,您会看到它源自 com.badlogic.gdx.utils.Json 第 912 行。

检查填充 json 字符串的代码,并确保它使用非空值填充它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 2022-11-29
    • 2014-06-26
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2021-05-06
    • 2017-05-10
    相关资源
    最近更新 更多