C
C#16mo ago
D12

❔ 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
SinFluxx
SinFluxx16mo ago
I don't think you can have { get; set; } present on your properties if you're using Unity's JsonUtility.FromJson
ChucklesTheBeard
ChucklesTheBeard16mo ago
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.
D12
D12OP16mo ago
thanks this seemed to have solved my issue
D12
D12OP16mo ago
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?
D12
D12OP16mo ago
@chucklesthebeard @sinfluxx
ChucklesTheBeard
ChucklesTheBeard16mo ago
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": [{...}] }
D12
D12OP16mo ago
ok thanks for the quick answer
D12
D12OP16mo ago
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.
D12
D12OP16mo ago
@chucklesthebeard
ChucklesTheBeard
ChucklesTheBeard16mo ago
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?
D12
D12OP16mo ago
when this is done i get the result "{}" @chucklesthebeard
D12
D12OP16mo ago
modified this and still get {}
SinFluxx
SinFluxx16mo ago
What about ...Jsonutility.FromJson<List<Server>>(UnserializedData); ?
D12
D12OP16mo ago
trying now
D12
D12OP16mo ago
D12
D12OP16mo ago
still nothing
SinFluxx
SinFluxx16mo ago
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", "
D12
D12OP16mo ago
ok thanks, i'll check it out
ChucklesTheBeard
ChucklesTheBeard16mo ago
Unity's JsonUtility does not support array as it is still new
(answered Mar 27, 2016) apparently still the case eh? when
SinFluxx
SinFluxx16mo ago
Just a small startup
Accord
Accord16mo ago
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.
D12
D12OP16mo ago
public static class JsonTester {
public static void Test() {
List<ServerInfo> playerInstances = new List<ServerInfo>();

ServerInfo playerInstance1 = new ServerInfo();
playerInstance1.ServerName = "8484239823";
playerInstance1.SavedPort = "Powai";
playerInstance1.SavedIP = "Random Nick";

ServerInfo playerInstance2 = new ServerInfo();
playerInstance2.ServerName = "512343283";
playerInstance2.SavedPort = "User2";
playerInstance2.SavedIP = "Rand Nick 2";
playerInstances.Add(playerInstance2);
playerInstances.Add(playerInstance1);
Log.Message(playerInstances[0].ServerName);
// To Json
string stringListAsJson = JsonUtility.ToJson(new JsonableListWrapper<ServerInfo>(playerInstances));
Log.Message(stringListAsJson);
// From Json
List<ServerInfo> stringListFromJson = JsonUtility.FromJson<JsonableListWrapper<ServerInfo>>(stringListAsJson).list;
Log.Message("Here");
//Convert to JSON
Log.Message(stringListFromJson.Count.ToString());
//Log.Message(stringListFromJson[0].ServerName);



}




}

[System.Serializable]
public class JsonableListWrapper<T>
{
public List<T> list;
public JsonableListWrapper(List<T> list) => this.list = list;
}
public static class JsonTester {
public static void Test() {
List<ServerInfo> playerInstances = new List<ServerInfo>();

ServerInfo playerInstance1 = new ServerInfo();
playerInstance1.ServerName = "8484239823";
playerInstance1.SavedPort = "Powai";
playerInstance1.SavedIP = "Random Nick";

ServerInfo playerInstance2 = new ServerInfo();
playerInstance2.ServerName = "512343283";
playerInstance2.SavedPort = "User2";
playerInstance2.SavedIP = "Rand Nick 2";
playerInstances.Add(playerInstance2);
playerInstances.Add(playerInstance1);
Log.Message(playerInstances[0].ServerName);
// To Json
string stringListAsJson = JsonUtility.ToJson(new JsonableListWrapper<ServerInfo>(playerInstances));
Log.Message(stringListAsJson);
// From Json
List<ServerInfo> stringListFromJson = JsonUtility.FromJson<JsonableListWrapper<ServerInfo>>(stringListAsJson).list;
Log.Message("Here");
//Convert to JSON
Log.Message(stringListFromJson.Count.ToString());
//Log.Message(stringListFromJson[0].ServerName);



}




}

[System.Serializable]
public class JsonableListWrapper<T>
{
public List<T> list;
public JsonableListWrapper(List<T> list) => this.list = list;
}
Log:
Requesting Server Info through packet
512343283
{}
Here
[Openworld] Failed to connect to server browser. Full Stack Error:
System.NullReferenceException: Object reference not set to an instance of an object
Requesting Server Info through packet
512343283
{}
Here
[Openworld] Failed to connect to server browser. Full Stack Error:
System.NullReferenceException: Object reference not set to an instance of an object
ServerInfo File
namespace OpenWorldRedux
{
[System.Serializable]
public class ServerInfo
{

public string ServerName;
public string SavedPort;
public string SavedIP;
}

}
namespace OpenWorldRedux
{
[System.Serializable]
public class ServerInfo
{

public string ServerName;
public string SavedPort;
public string SavedIP;
}

}
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
SinFluxx
SinFluxx16mo ago
I'm not sure myself, is it easier just to try and use newtonsoft or something at this point?
D12
D12OP16mo ago
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
Accord
Accord16mo ago
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.
D12
D12OP16mo ago
no
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server