2017-07-26 13 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,这个问题在一个稍微不同的上下文中。