我今天第一次使用GSON。现在我正在尝试使用值反序列化序列化类。 Deserialise完美地工作,但在加载时它只输出所有字段为null。我真的想知道我做错了什么,或者为什么它不起作用。
要填写的课程:
public class PlayerData {
private static Map<UUID, PlayerData> playerDataMap = new HashMap<UUID, PlayerData>();
public UUID uuid;
public Integer kills;
public Integer level;
public Integer xp;
public SkillType skillType;
public PlayerData() {
playerDataMap.put(getUUID(), this);
String logString = "[DEBUG] " + uuid + " " + skillType + " " + level;
ZombieSurvival.getInstance().getLogger().info(logString);
}
}
保存代码:
private synchronized void savePlayerData(){
try {
Map<UUID, PlayerData> map = PlayerData.getPlayerDataMap();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String seperator = File.separator;
String path = getDataFolder().getAbsolutePath() + seperator + "PlayerData" + seperator;
File folder = new File(path);
folder.mkdirs();
for(UUID uuid : map.keySet()){
PlayerData pd = map.get(uuid);
String json = gson.toJson(pd);
File myFile = new File(folder,uuid.toString() + ".json");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append(json);
myOutWriter.close();
fOut.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
信息:JSON内容是由此方法创建的!
加载代码:
private void loadPlayerData() {
try {
//Setup GSON Loading
String seperator = File.separator;
File invasionMobFolder = new File(getDataFolder() + seperator + "PlayerData" + seperator);
invasionMobFolder.mkdirs();
File[] listOfFiles = invasionMobFolder.listFiles();
for (File file : listOfFiles) {
if (file.getName().endsWith(".json")) {
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = ""; //Holds the text
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow;
}
myReader.close();
Gson gson = new Gson();
gson.fromJson(aBuffer, PlayerData.class);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSON内容:
{
"uuid": "ba79c71f-aa2c-4f8b-a7e5-d6bfea7a4dcc",
"kills": 0,
"level": 0,
"xp": 0,
"skillType": "KNIGHT"
}
答案 0 :(得分:1)
正如Steve Bishop所说或试图说的那样,import Foundation
func flatten< T, U >( _ elem: T ) -> [ U ] {
if let item = elem as? U {
return [ item ]
} else {
return []
}
}
func flatten< T, U >( _ ary: [ T ] ) -> [ U ] {
if ary.count == 0 {
return []
}
return flatten( ary.first ) + flatten( Array( ary.dropFirst() ) )
}
let x: [ Int ] = flatten( [ 1, 2, [ 3, 4 ], [ [ 5, 6 ], nil, 7 ], 8, 9 ] )
print( x ) // Prints [1, 2, 8, 9 ] instead of [1, 2, 3, 4, 5, 6, 7, 8, 9]
中gson.fromJson
方法调用的返回值未被使用。如果要将PlayerData对象保存为静态变量,则无法在每个PlayerData类的构造函数中执行此操作,因为GSON很可能首先调用构造函数,然后使用您的匹配字段填充变量json字符串通过反射导致您的空填充字段。
您需要做的是从loadPlayerData
获取反序列化的JSON并将其放入您的静态gson.fromJson
字段中,该字段显然至少需要一个公共设置器。
答案 1 :(得分:0)
您的字段是私有的,GSON默认只映射公共字段。
答案 2 :(得分:0)
您没有保存结果。
private List<PlayerData> loadPlayerData(){
List<PlayerData> players = new ArrayList<>();
try {
//Setup GSON Loading
String seperator = File.separator;
File invasionMobFolder = new File(getDataFolder() + seperator + "PlayerData" + seperator);
invasionMobFolder.mkdirs();
File[] listOfFiles = invasionMobFolder.listFiles();
for(File file : listOfFiles){
if(file.getName().endsWith(".json")){
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = ""; //Holds the text
while ((aDataRow = myReader.readLine()) != null)
{
aBuffer += aDataRow ;
}
myReader.close();
Gson gson = new Gson();
players.add(gson.fromJson(aBuffer, PlayerData.class));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return players;
}