【问题标题】:convert json to C# class将 json 转换为 C# 类
【发布时间】:2022-01-15 20:43:59
【问题描述】:

我想将以下内容转换为 C# 类,有人可以帮忙吗?我尝试了许多不同的突变,但没有一个能真正将Params 映射到类。

{
    "method": "update",
    "params": [
        true,
        {
            "y": [
                [
                    "3",
                    "5"
                ],
                [
                    "1",
                    "2"
                ]               
            ],
            "x": [
                [
                    "1",
                    "2"
                ],
                [
                    "2",
                    "1"
                ]
            ],
            "id": 1111,
            "update": 164227,
            "current": 164227
        },
        "TESTING"
    ],
    "id": null
}

到目前为止,我可以生成以下内容,但不幸的是,它无法将Params 映射到类,我想在列表中获取XY 的值:

public class Data
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("params")]
    public List<ParamTest> Params { get; set; }

    [JsonProperty("id")]
    public object Id { get; set; }
}

public class ParamClass
{
    [JsonProperty("x")]
    public string[][] X { get; set; }

    [JsonProperty("x")]
    public string[][] Y { get; set; }
}

public struct ParamTest
{
    public bool Bool;
    public List<ParamClass> ParamClass;
    public string String;

    public static implicit operator ParamTest(bool Bool) => new ParamTest { Bool = Bool };
    public static implicit operator ParamTest(List<ParamClass> ParamClass) => new ParamTest { ParamClass = ParamClass };
    public static implicit operator ParamTest(string String) => new ParamTest { String = String };
}

【问题讨论】:

  • @Steve 我尝试了所有这些在线工具。但都不起作用!快速打字。 json2csharp,还有 10 个..
  • 您是否尝试过为该属性编写自定义转换器
  • 嗯,QuickType 真的没有为它提供工作类吗?在我看来,当我将一个混合数组放入其中时,它正在编写大量合理的自定义 deser。i.stack.imgur.com/qNu3v.jpg
  • (关于“QuickType 不起作用”的说法,我不得不承认我在制作上面的截图时没有测试它(我在手机上),但是你喜欢吗它说在生成代码顶部的 cmets 中?即使用他们生成的 FromJson 帮助器来注册生成的自定义转换器等,或者您是否在没有自定义转换器的情况下以基本的 JsonConvert.DeserializeObject&lt;SomeQTClass&gt;(str) 方式使用它..)跨度>

标签: c# json serialization deserialization json-deserialization


【解决方案1】:

试试这个。它在 Visual Studio 中经过测试并正常工作

    var po = JObject.Parse(json); 

    Data data = new Data { Id = (string)po["id"], Method = (string)po["method"] 

    var pars = new ParamObject { };

    foreach (var element in (JArray)po["params"])
    {
        var type = element.GetType().Name;
        if (element.GetType().Name == "JValue")
        {
            if ((string)element == "TESTING")
                pars.Testing = (string)element;
            else pars.IsTrue = (bool)element;
        }
        else if (element.GetType().Name == "JObject")
        {
            pars.XYElement = element.ToObject<XYElement>();
        }
    }
    data.Pars = pars;

public partial class Data
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("params")]
    public ParamObject Pars { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }
}

public partial class ParamObject
{
    [JsonProperty("XYElement")]
    public XYElement XYElement { get; set; }

    [JsonProperty("IsTrue")]
    public bool IsTrue { get; set; }

    [JsonProperty("TESTING")]
    public string Testing { get; set; }
}
public partial class XYElement
{
    [JsonProperty("y")]
    public List<List<long>> Y { get; set; }

    [JsonProperty("x")]
    public List<List<long>> X { get; set; }

    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("update")]
    public long Update { get; set; }

    [JsonProperty("current")]
    public long Current { get; set; }
}

【讨论】:

    【解决方案2】:

    由于非结构化数据结构,必须使用Object进行反序列化。

    使用以下类

    public class JsonData
    {
        public string Method { get; set; }
        public List<Object> Params { get; set; }
        public int? ID { get; set; }
    }
    public class Params
    {
       public string[][] X { get; set; }
       public string[][] Y { get; set; }
    }
    

    那么现在使用

    var data = JsonConvert.DeserializeObject<JsonData>(json);
    var paramsData = JsonConvert.DeserializeObject<Params>(data.Params[1].ToString());
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多