C
C#12mo 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
SinFluxx12mo ago
I don't think you can have { get; set; } present on your properties if you're using Unity's JsonUtility.FromJson
ChucklesTheBeard
ChucklesTheBeard12mo 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
D1212mo ago
thanks this seemed to have solved my issue
D12
D1212mo 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
D1212mo ago
@chucklesthebeard @sinfluxx
ChucklesTheBeard
ChucklesTheBeard12mo 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
D1212mo ago
ok thanks for the quick answer
D12
D1212mo 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
D1212mo ago
@chucklesthebeard
ChucklesTheBeard
ChucklesTheBeard12mo 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
D1212mo ago
when this is done i get the result "{}" @chucklesthebeard
D12
D1212mo ago
modified this and still get {}
SinFluxx
SinFluxx12mo ago
What about ...Jsonutility.FromJson<List<Server>>(UnserializedData); ?
D12
D1212mo ago
trying now
D12
D1212mo ago
D12
D1212mo ago
still nothing
SinFluxx
SinFluxx12mo 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
D1212mo ago
ok thanks, i'll check it out
ChucklesTheBeard
ChucklesTheBeard12mo ago
Unity's JsonUtility does not support array as it is still new
(answered Mar 27, 2016) apparently still the case eh? when
SinFluxx
SinFluxx12mo ago
Just a small startup
Accord
Accord12mo 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
D1212mo 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
SinFluxx12mo ago
I'm not sure myself, is it easier just to try and use newtonsoft or something at this point?
D12
D1212mo 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
Accord12mo 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
D1212mo ago
no
Accord
Accord12mo 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
More Posts
❔ How can I read data from file called lang.json```C# using System; using System.Collections.Generic; using CitizenFX.Core; using Newtonsoft.Json; u✅ ByBit .NetI have a problem with bybit.net, when I try to get acc balance it successfully sends me data but whe❔ C# Web browser change tabs = index 0 is out of range exception.I am making a web browser and every time I change the selected tab, it gives me an index 0 is out of❔ Change default behaviour of .NET Web API Validation ErrorsHi does anyone know how I can override the default problem details/validation error response .net gi❔ How would you refactor something like this? Is there a pattern that could help me?Is there a way to clean something like this up? I've got a lot of evaluations to perform based on a ❔ C# library usable from pythonI was wondering how to do this, and stumbled upon https://stackoverflow.com/a/29854281 which answere❔ Is there a pattern for having a single collection of different object types?This is my sample code https://dotnetfiddle.net/ffV3vY I have a WorkflowCommand a ScheduledWorkflowC❔ How to create WPF installer?Hello, I want to create installer.exe file using WPF, so i have custom GUI. I want to copy files froASK beginnerI'm confused about it. Could someone please explain it? Should I check mark it? or when I'll mark ❔ ✅ Error MSB4018 : The "ResolvePackageAssets" task failed unexpectedly. When building.Full Stacktrace: Use build tool: C:\Program Files\dotnet\sdk\8.0.100-preview.5.23303.2\MSBuild.dll