Deserialize JSON object array in C# using LitJson -
i'm limited unity3d 3 new security measures using litjson web player application, otherwise won't compile.
is there simple way serialize output object (contents of userecoget.text):
{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}
this code enters first object json:
using unityengine; using system.collections; using litjson; ... public ecodata localecodata; ... localecodata = jsonmapper.toobject<ecodata>(userecoget.text); ... public class ecodata { public string building_id; public string building_level; public string workers; public string building_health; }
turning localecodata array leads missing constructor errors. , don't know how nest constructor correctly in situation, not sure if even.
at point i'm resorting using jsonreader , filling in object manually. appreciated. thank you.
edit: that's crazy, killed day today because of crappy json formatting inflicted on myself...
this works now, localecodata[1,2...32] object array elements accessible:
public ecodata[] localecodata; ... localecodata = jsonmapper.toobject<ecodata[]>(userecoget.text);
all had compose json correctly on php page handles mysql , transfer unity in orderly fashion. echoing directly without using array transfer. output looks , works great:
[{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}]
thanks, man!
i suspect problem json itself. not in correct format. parse json array need have in format:
{ result:[ {"building_id":"1","building_level":"0","workers":"0","building_health":"100"}, {"building_id":"2","building_level":"0","workers":"0","building_health":"100"},... {"building_id":"32","building_level":"0","workers":"0","building_health":"100"} ] }
if dont have control on json may need split string , treat each string single json object , convert each element in array c# object type.
Comments
Post a Comment