I am trying to print json api data, but the classes name are “1”, “2”, “3”. But the C# don’t accepts to reference numbers.
Can someone help me?
I am trying to print json api data, but the classes name are “1”, “2”, “3”. But the C# don’t accepts to reference numbers.
Can someone help me?
Don’t name your classes with numbers. Use One, Two, Three, or even better, give them useful names.
But the classes are from a web Json API, and it’s not mine. So I can’t change the name to get the data. I was trying to use strings to reference the classes as “one, two…” but don’t run.
Json doesn’t have class names, so how is this even an issue?
Do you mean property names are numbers (which C# also doesn’t allow)?
Can you show us an example of the json?
The class
Trying to reference 1
Do you Know what can I do to print classes with numbers?
First off… you can copy your code into the forum with the code tags. Rather than doing it as images. See:
…
With that said…
That “1” is not a class, it’s a property, and the property contains an object.
What you’re asking for is the ability to deserialize properties with numeric names. But C# doesn’t support numerically named properties. So you’ll need a way to tell the serialization engine to rename the property.
So this hinges on the serialization engine you use. If you’re using the built in JsonUtility from Unity there’s not really a lot you can do. Unity technically has the ‘FormerlySerializedAs’ attribute, but it doesn’t work for your situation.
If you go and get Json.Net though, you can use the JsonProperty attribute to define the name of the property like so:
[System.Serializable]
public class APIData
{
[JsonProperty("1")]
public Maquina_Um One { get; set; }
}
Json.Net is available as a package for unity:
Newtonsoft Json Unity Package | Newtonsoft Json | 3.0.2
Here’s an example of it working for some generic json:
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Serialization;
public class zTest01 : MonoBehaviour
{
private void Update()
{
if (!Input.GetKeyDown(KeyCode.Space)) return;
var json = "{\"1\":{\"Id\":\"Test\",\"Value\":5},\"2\":{\"Id\":\"Dyldo\",\"Value\":6}}";
//var obj = JsonUtility.FromJson<Foo>(json); //does not work
var obj = JsonConvert.DeserializeObject<Foo>(json);//works
Debug.Log(obj?.One?.Id);
Debug.Log(obj?.One?.Value);
Debug.Log(obj?.Two?.Id);
Debug.Log(obj?.Two?.Value);
}
[System.Serializable]
private class Foo
{
[FormerlySerializedAs("1")]//does not work
[JsonProperty("1")]//works
public Bar One;
[FormerlySerializedAs("2")]//does not work
[JsonProperty("2")]//works
public Bar Two;
}
[System.Serializable]
private class Bar
{
public string Id;
public int Value;
}
}
Note that such a format though implies there might be a variable number of properties (1,2,3…N). It might be better to use something like a JObject to deserialize each entry:
In fact, that looks like an associative array (similar to C# dictionary) which was populated as an array, but then serialized as an object.
You can try and rebuild that JSON so that it enumerates a proper array using some formatter.
The whole difference is this
{ “something” : { “1” : { content1 }, “2” : { content2 } } } —> { “myArray” : [ { content1 }, { content2 } ] }
But for that to work, I’m guessing your leaf objects need to match field-wise. Man, higher languages have such a carefree happy time.
Using json.net, isn’t this something that could just be cast into a dictionary<string, someClass> ?
That’s what it looks like to me honestly.
Dictionary<string, AClass> bDict = JsonConvert.DeserializeObject<Dictionary<string, AClass>>(aString);
AClass would just be whatever the class name is and just have properties setup for all those values. Assuming we’re seeing enough of the json there, it would work.