2014-04-20 64 views
1

我查看了大部分在線的可用教程,在Libgdx中使用Json,這看起來非常簡單。但是,由於某種原因,json.toJson()不斷返回一個兩個單元格的數組,第一個包含{和第二個包含}。 我的代碼是非常簡單的:Libgdx,JSON:Json.tojson(object)不斷返回{}

public class GameConfig { 
    public int screenWidth; 
    public int screenHeight; 

    public GameConfig() { 
     screenWidth = 800; 
     screenHeight = 600; 
    } 
} 

然後

String configAsText = json.toJson(zzz);//configAsText.value equals [0] = { and [1] = } 
String configAsText = json.toJson(config, GameConfig.class);//nor this one 

任何想法?

+0

如何實例化json對象? –

+0

你知道JSON是什麼嗎?它不是「兩個單元格數組」,而是代表JSON中空對象的String ['}'。 – noone

+0

Christian Cederquist < - 像這樣Json json = new Json(); – coffeenet

回答

5

請嘗試以下代碼。我以這種方式使用它,在大多數情況下,我不需要任何自定義序列化器,因爲libgdx用於序列化的內部反射。

Json json = new Json(); 

json.setTypeName(null); 
json.setUsePrototypes(false); 
json.setIgnoreUnknownFields(true); 
json.setOutputType(OutputType.json); 

json.toJson(config, GameConfig.class); 
+0

工作,thanx! :) – coffeenet

+0

這可以與fromjson()一起使用嗎? – coffeenet

+0

@coffeenet如果您可以將它轉換爲JSON而無需特殊的序列化器,那麼您應該也可以將其轉換回來。 – noone

相關問題