Proper way to deserialize JSON to object in C# Unity 5? -
this question has answer here:
i developing simple unity game in c#. want send json data forth between unity , database. using unity's jsonutility
deserialize, having trouble doing correctly.
here's 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" } }
here's code create object:
rootobject res = rootobject.createfromjson(www.text); debug.log(res.table.tablename);
here classes:
[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); } }
the issue when call debug.log(res.table.tablename);
, error below.
error:
nullreferenceexception: object reference not set instance of object
i'm sorry guys. noob @ stackoverflow , c# in general.
turns out question has been answered here, question being under different context.
Comments
Post a Comment