如何从 C# 创建不同数据类型的列表或数组以保存到 Unity 中的 JSON

发布于 2025-01-18 18:34:26 字数 1080 浏览 2 评论 0原文

我希望我的输出 JSON 包含一个如下所示的简单数组

{
"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
        }
    ]
}

请注意数组中最后一项与前面一项的不同之处。与之前的字符串条目相比,名为“value”的变量也是一个整数。

我该如何才能输出如上所示的 JSON?我尝试创建一个可以存储所有信息的类,但我无法为 int 和 string 声明重用名称“value”,并且也不希望在变量值为 null 时显示变量 (示例如下所示)

{
  "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
    }
  ]
}

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
    }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑白记忆 2025-01-25 18:34:26

您可以使用对象类型。

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; }
}

输出:

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

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"
    }
  ]
}
神回复 2025-01-25 18:34:26

尝试此

        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);

课程

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; }
}

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文