Home > Software design >  How do I create a List or Array of different data types from C# to be saved to JSON in Unity
How do I create a List or Array of different data types from C# to be saved to JSON in Unity

Time:04-05

I would like my output JSON to contain a simple array shown below

{
"attributes":[
        {
            "trait_type": "Background",
            "value": "Green"
        },
        {
            "trait_type": "Body",
            "value": "Body_1"
        },
        {
            "trait_type": "Outfit",
            "value": "Beach_Singlet"
        },
        {
            "display_type":"date",
            "trait_type":"birthday",
            "value":869270400
        }
    ]
}

Notice how the last item is different from the previous items in the array. The variable named "value" is also an integer as compared to the previous entries as strings.

How do I go about in order to be able to output my JSON as shown above? I have tried creating a class that can store all the information, but I cannot reuse the name "value" for both an int and string declaration, and also do not wish to show the variables if their value is null (Example shown below)

{
  "attributes": [
    {
      "display_type": "",
      "trait_type": "Background",
      "value": "Green"
    },
    {
      "display_type": "",
      "trait_type": "Body",
      "value": "Body_1"
    },
    {
      "display_type": "",
      "trait_type": "Outfit",
      "value": "Beach_Singlet"
    },
    {
      "display_type": "date",
      "trait_type": "birthday",
      "value": 869270400
    }
  ]
}

CodePudding user response:

try this

        var attributes=new List<Attribute>{
        new AttributeString{
            trait_type="Background",
            value="green"
        },
        new AttributeInt{

            display_type ="date",
            trait_type="birthday",
            value=869270400
        }
    };

    var jsonSerializerSettings = new JsonSerializerSettings()
    {
        TypeNameHandling = TypeNameHandling.Objects,
        NullValueHandling=NullValueHandling.Ignore,
        Formatting=Newtonsoft.Json.Formatting.Indented
    };

    var json = JsonConvert.SerializeObject(attributes,jsonSerializerSettings);

classes

public class Attribute
{
    public string trait_type { get; set; }

    public string display_type { get; set; }
}
public class AttributeString:Attribute
{
        public string value { get; set; }
}
public class AttributeInt:Attribute
{
        public int value { get; set; }
}

public class AttributeList
{
    public List<Attribute> attributes { get; set; }
}

CodePudding user response:

You can use object type.

using Newtonsoft.Json;

var list = new AttributeList 
{
    attributes = new []{
        new Attribute
        {
            trait_type = "Background",
            value = "green"
        },
        new Attribute
        {
            display_type = "date",
            trait_type = "birthday",
            value = 869270400
        }
    } 
};

var json = JsonConvert.SerializeObject(list, Formatting.Indented);
Console.WriteLine(json);

public class Attribute
{
    public object value { get; set; }

    public string trait_type { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string display_type { get; set; }
}

public class AttributeList
{
    public Attribute[] attributes { get; set; }
}

Output:

  {
  "attributes": [
    {
      "value": "green",
      "trait_type": "Background"
    },
    {
      "value": 869270400,
      "trait_type": "birthday",
      "display_type": "date"
    }
  ]
}
  • Related