❔ Basic C# Getting Null Result when trying to index attribute of an object.
I need some help here as im not sure what im doing wrong, the first screenshot is the code being run leading to the error, and the second screenshot is a seperate file that is the structure of the object. When i use the JsonUtility to turn the json string into the object i cant index the attribute ServerName. The provided Json string is
{"ServerName":"Server","SavedPort":"25555","SavedIP":"127.0.0.1"}
. Thanks for the time for looking at this.27 Replies
I don't think you can have
{ get; set; }
present on your properties if you're using Unity's JsonUtility.FromJson
https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html only says it allows fields (as in
public string ServerName;
), it doesn't mention properties (as in public string ServerName{ ... };
) so presumably they're not supported.thanks this seemed to have solved my issue
I changed it a little bit to allow a list of servers to be sent however now im getting a similar error. From what i've seen this should get me the result im hoping for but it doesnt. Can i not convert a list of objects in a json string to a list of objects using json utility from json?
@chucklesthebeard @sinfluxx
googling the error message reveals that it doesn't support arrays as the base type; you'll need to wrap it in something like
{ "Servers": [{...}] }
ok thanks for the quick answer
I did this so that the json string was
{"Servers":[{"ServerName":"Server","SavedPort":"127.0.0.1","SavedIP":"25555"}]}
however the count of the list "Servers" is 0 for some reason.@chucklesthebeard
does it work the other way around?
(can you feed a ServerInfo into ToJson)
If that works, no idea. If it doesn't, also no idea, but you might be able to figure it out.
wonder if it just doesn't like the nested class?
when this is done i get the result "{}" @chucklesthebeard
modified this and still get {}
What about
...Jsonutility.FromJson<List<Server>>(UnserializedData);
?trying now
still nothing
Sounds like this may be a better answer https://stackoverflow.com/a/36244111
Stack Overflow
Serialize and Deserialize Json and Json Array in Unity
I have a list of items send from a PHP file to unity using WWW.
The WWW.text looks like:
[
{
"playerId": "1",
"playerLoc": "Powai"
},
{
"playerId": "2",
"
ok thanks, i'll check it out
Unity's JsonUtility does not support array as it is still new(answered Mar 27, 2016) apparently still the case eh?
Just a small startup
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Log:
ServerInfo File
I have tried a bunch of stuff from this solution but it seems nothing works
(I have also tried the json helper but i get the same result)
so im really not sure what im doing wrong
I'm not sure myself, is it easier just to try and use newtonsoft or something at this point?
yeah i did try to use newton soft but this is for a rimworld mod and im not really sure what newtonsoft dll im supposed to be using
and kept getting errors
ah it was this error
System.TypeLoadException: Could not resolve type with token 010000a1
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.no
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.