Java 对JSON的解析不正确
我有HashMap:Java 对JSON的解析不正确,java,json,hashmap,Java,Json,Hashmap,我有HashMap: public static volatile HashMap<String, GooglePlayGame> games = new HashMap<>(); 然后我打开JSON文件: { "GRID™ Autosport": { "Title": "GRID™ Autosport", "Last date of update": "11 December, 2019", "Current version": "1.6.
public static volatile HashMap<String, GooglePlayGame> games = new HashMap<>();
然后我打开JSON文件:
{
"GRID™ Autosport": {
"Title": "GRID™ Autosport",
"Last date of update": "11 December, 2019",
"Current version": "1.6.1RC2-android",
"Required Android": "no info",
"Genre": "Racing",
"Price": "10,00$",
"IAP": "nothing",
"Dev email": "googleplay@feralinteractive.com"
}
}
{
"FRAMED 2": {
"Title": "FRAMED 2",
"Last date of update": "11 July, 2019",
"Current version": "1.1.1",
"Required Android": "5.0+",
"Genre": "Puzzle",
"Price": "2,00$",
"IAP": "nothing",
"Dev email": "support+framed2@noodlecake.com"
}
}
但我得到了错误“JSON标准只允许一个顶级值”
如何修复此问题?这不是正确的JSON格式。这两个对象应该在一个列表
[{},{}]
或一个对象{{},{}}
中,如何使用Gson修复它?我应该把我的游戏对象放到ArrayList或者什么?因为你在FileWriter中使用的是append模式(filename,true),所以看起来你只是在把JSON附加到文件中。在调用GSON.toJson()之前是否创建了新的HashMap?我使用了GSON,切换到Jackson,现在它工作正常。在调用toJson之前,我没有创建新映射。我刚刚在这张地图上添加了所有新的对“键值”。每次添加后,我都会将映射的新版本记录到同一个json中。
{
"GRID™ Autosport": {
"Title": "GRID™ Autosport",
"Last date of update": "11 December, 2019",
"Current version": "1.6.1RC2-android",
"Required Android": "no info",
"Genre": "Racing",
"Price": "10,00$",
"IAP": "nothing",
"Dev email": "googleplay@feralinteractive.com"
}
}
{
"FRAMED 2": {
"Title": "FRAMED 2",
"Last date of update": "11 July, 2019",
"Current version": "1.1.1",
"Required Android": "5.0+",
"Genre": "Puzzle",
"Price": "2,00$",
"IAP": "nothing",
"Dev email": "support+framed2@noodlecake.com"
}
}