- Home /
C# Converting json arrays using JsonUtility,
hey all! I'm currently trying to convert an array from a json file to a c# array. I understand that it has to be parsed into a class structure like such:
Here is my Json file:
{
"messages": [
{
"recipient_id": "Player1",
"custom": {
"Animate": "Start",
"Text": "Say hi"
}
}
]
}
And here is my class structures:
[Serializable]
public class JsonClass
{
public Messages[] messages;
}
[Serializable]
public class Messages{
public string recipient_id;
public Custom custom;
}
[Serializable]
public class Custom
{
public string Animate;
public string Text;
}
I parse the json string using JsonUtility, and tried to print out the elements as such:
public IEnumerator ParseMessage(string myJsonString){
JsonClass MyJsonClass = JsonUtility.FromJson<JsonClass>(myJsonString);
var id = MyJsonClass.messages[0];
Debug.Log($"Players id: {id.recipient_id}");
var texts = MyJsonClass.messages[1].custom.Text;
Debug.Log($"Text: {texts}");
var animates = MyJsonClass.messages[1].custom.Animate;
Debug.Log($"Animate: {animates}");
yield return null;
}
I can get recipient_id to print, but not Text and Animate. Is this an issue with my class structure?
Any help is really appreciated! (Losing a lot of sleep over this),
I think it's because you are trying to take messages[1] ins$$anonymous$$d of messages[0] for text and animate variables.
Omg, thanks so much you're a lifesaver! That definitely worked:) Just to clarify, why wouldn't public Custom custom be in $$anonymous$$essages[1] ins$$anonymous$$d of $$anonymous$$essages[0]? Is it because an array of JSON objects is denoted by square brackets [], and elements within the square brackets are separated by commas?
It is because C# arrays and lists are 0 based.
Answer by akohl0175 · Mar 21, 2020 at 10:45 AM
Not an issue with class structure! Made the mistake of calling Message[1] when it is actually Message[0]. Also, JSONUtility is unable to parse the Json objects into the class structures. Only by using JSON.Net was I able to. (https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347)
The new code is as such:
using Newtonsoft.Json;
...
public IEnumerator ParseMessage(string myJsonString){
JsonClass MyJsonClass = JsonConvert.DeserializeObject<JsonClass>(myJsonString);
var id = MyJsonClass.messages[0];
Debug.Log($"Players id: {id.recipient_id}");
var texts = MyJsonClass.messages[0].custom.Text;
Debug.Log($"Text: {texts}");
var animates = MyJsonClass.messages[0].custom.Animate;
Debug.Log($"Animate: {animates}");
yield return null;
}
I'm pretty sure you could use JsonUtility, if you created an instance of your class and then serialized it, you should've been able to deserialize again that serialized string to receive your JsonClass object.
Your answer
Follow this Question
Related Questions
C# Convert json arrays to unity arrys 3 Answers
Help using LitJson 1 Answer
Instantiate a prefab from jsonarray? 1 Answer
Constructing object array with JsonUtility 1 Answer
Error: CS0200 | Read only array? 1 Answer