gpt4 book ai didi

android - java.lang.IllegalStateException : Expected BEGIN_OBJECT but was STRING at line 1 column 错误

转载 作者:行者123 更新时间:2023-11-29 19:01:42 28 4
gpt4 key购买 nike

我正在学习 parcing,但在尝试实现它时遇到了问题。发现了很多类似的问题和更多的解决方案,但没有任何帮助

那是我的 json

{
"firstName": "name",
"lastName": "last",
"resistances": {
"a1": 1,
"a2": 2,
"a3": 3,
"a4": 4
}
}

播放器类

class Player implements Serializable{

String firstName;
String lastName;
int[] resistances;

public Player(String firstName, String lastName, int[] resistances) {
this.firstName = firstName;
this.lastName = lastName;
this.resistances = resistances;
}
}

以及我是如何尝试的

Gson gson = new Gson();
Player player = gson.fromJson("test.json", Player.class);

求救

最佳答案

fromJson(String, Class)解析字符串本身,在你的例子中是 test.json,而不是它代表的文件。您需要从您尝试解析的文件创建一个 Reader,并将其作为第一个参数传递。

如果 test.json 在外部存储中:

String fpath = Environment.getExternalStorageDirectory() + "/path/to/test.json";
try {
FileReader fr = new FileReader(fpath));
gson.fromJson(fr, Player.class);
} catch (Exception e1) {
e1.printStackTrace();
}

关于android - java.lang.IllegalStateException : Expected BEGIN_OBJECT but was STRING at line 1 column 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845334/

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