2016-11-11 855 views
2

這是我的樣本JSON:使用JsonUtility.FromJson反序列化JSON在Unity

{"data":[{"id":141,"layoutLabel":"Sameer","hasCustomProb":1}, 
{"id":214,"layoutLabel":"abc","hasCustomProb":0}],"status":200} 

這是我做

[System.Serializable] 
public class PlayerInfo 
{ 
    public string [] data; 
    public int status; 
} 

類這是我如何得到 「狀態」 從JSON:

PlayerInfo P = JsonUtility.FromJson<PlayerInfo>(json); 
Debug.Log(P.status) //returns 200 

有人可以幫助我,我可以得到並保存數據數組或可能會得到data.id和data.hasCustomProb?我是C#和團結的新手。

+0

數據不字符串型。在您的示例JSON中,它是一個包含「id」,「layoutLabel」和「hasCustomProb」屬性的對象數組。 – Lithium

回答

3

你的類應該是這樣的

[System.Serializable] 
public class PlayerInfo 
{ 
    public List<ActData> data; 
    public int status; 
} 

[System.Serializable] 
public class ActData 
{ 
    public int id; 
    public string layoutLabel; 
    public int hasCustomProb; 
} 
+1

除了你必須刪除'{get;組;來自所有變量。 'JsonUtility'不能用於此。 – Programmer

+1

@程序員你是對的。我想確定是否需要'Serializable'屬性,因爲我剛剛編輯了這個答案? –

+0

是的,那太或OP會得到空的結果。 – Programmer