2017-07-26 38 views
-1

我正在用C#開發簡單的Unity遊戲。我想在Unity和數據庫之間返回JSON數據。我正在使用Unity的JsonUtility進行反序列化,但我無法正確執行此操作。正確的方式反序列化JSON到C#Unity 5中的對象?

這裏是我的JSON:

{ 
    "Table": { 
     "AttributeDefinitions":[ 
     { 
      "AttributeName":"pleasework", 
      "AttributeType":"S" 
     } 
    ], 
    "CreationDateTime":1.501013735355E9, 
    "ItemCount":0, 
    "KeySchema":[ 
     { 
      "AttributeName":"pleasework", 
      "KeyType":"HASH" 
     } 
    ], 
    "ProvisionedThroughput":{ 
     "NumberOfDecreasesToday":0, 
     "ReadCapacityUnits":5, 
     "WriteCapacityUnits":5 
    }, 
    "TableArn":"stringhere", 
    "TableId":"stringhere", 
    "TableName":"stringhere", 
    "TableSizeBytes":0, 
    "TableStatus":"ACTIVE" 
    } 
} 

這裏是我的代碼來創建對象:

RootObject res = RootObject.CreateFromJSON(www.text); 
Debug.Log(res.Table.TableName); 

這裏是類:

[System.Serializable] 
public class AttributeDefinition 
{ 
    public string AttributeName { get; set; } 
    public string AttributeType { get; set; } 
} 

[System.Serializable] 
public class KeySchema 
{ 
    public string AttributeName { get; set; } 
    public string KeyType { get; set; } 
} 

[System.Serializable] 
public class ProvisionedThroughput 
{ 
    public int NumberOfDecreasesToday { get; set; } 
    public int ReadCapacityUnits { get; set; } 
    public int WriteCapacityUnits { get; set; } 
} 

[System.Serializable] 
public class Table 
{ 
    public List<AttributeDefinition> AttributeDefinitions { get; set; } 
    public double CreationDateTime { get; set; } 
    public int ItemCount { get; set; } 
    public List<KeySchema> KeySchema { get; set; } 
    public ProvisionedThroughput ProvisionedThroughput { get; set; } 
    public string TableArn { get; set; } 
    public string TableId { get; set; } 
    public string TableName { get; set; } 
    public int TableSizeBytes { get; set; } 
    public string TableStatus { get; set; } 
} 

[System.Serializable] 
public class RootObject 
{ 
    public Table Table { get; set; } 
    public static RootObject CreateFromJSON(string json) 
    { 
     return JsonUtility.FromJson<RootObject>(json); 
    } 
} 

問題是我打電話給Debug.Log(res.Table.TableName);,我得到下面的錯誤。

錯誤:

NullReferenceException: Object reference not set to an instance of an object

回答

0

,我對不起你們。 Noob在Stackoverflow和C#一般。

原來我的問題已經回答here,這個問題在一個稍微不同的上下文中。