福利和生成的C#类的缺点为JSON对象 [英] Benefits and drawbacks of generated C# classes for Json objects

查看:143
本文介绍了福利和生成的C#类的缺点为JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有样JSON和我需要将其序列化到C#对象。我决定利用为此 Json.Net 库。我也需要有C#类,这将重新present此JSON。创建类可以用来 Json的C#类生成。在那里,我们有两个选择。 创建属性,生成的类会是这样的:

I have sample Json and I need to serialize it into C# objects. I decided to leverage for this purpose Json.Net library. Also I need to have C# classes which will represent this Json. To create classes could be used Json C# class generator. There we have two options. "Create properties" and generated classes will look like:

public class Address
{
    private JObject __jobject;
    public Address(JObject obj)
    {
        this.__jobject = obj;
    }
    public string street_address
    {
        get
        {
            return JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "street_address"));
        }
    }
    public string city
    {
        get
        {
            return JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "city"));
        }
    }
    public string state_province
    {
        get
        {
            return JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "state_province"));
        }
    }
    public string zip_postal_code
    {
        get
        {
            return JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "zip_postal_code"));
        }
    }
}

和另一个选项是生成pre-填充只读域和类看起来像

and another option is "Generate pre-populated read-only fields" and classes will look like

public class Address
{

    public Address(JObject obj)
    {
       this.street_address = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "street_address"));
       this.city = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "city"));
       this.state_province = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "state_province"));
       this.zip_postal_code = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(obj, "zip_postal_code"));
    }

    public readonly string street_address;
    public readonly string city;
    public readonly string state_province;
    public readonly string zip_postal_code;
}

这两个生成的类依靠JObject和JsonClassHelper。但这些类不能与JsonSerializer用于像

Both these generated classes rely on JObject and JsonClassHelper. But these classes could not be used with JsonSerializer like

var ro = jsonSerializer.Deserialize<RootObject>(reader);

我们可以创建一个使用JObject.Load方法,这些类的对象

We can create objects of these classes using JObject.Load method

var ro = new RootObject(Newtonsoft.Json.Linq.JObject.Load(reader));

另一种方法是使用网上 json2csharp 转换器和类看起来像

public class Address
{
    public string street_address { get; set; }
    public string city { get; set; }
    public string state_province { get; set; }
    public string zip_postal_code { get; set; }
}

JsonSerializer可以处理此类。搜索结果
我的问题是哪些类发生器是preferable使用,有什么好处和使用每种类型生成的类的缺点?结果
谢谢您的建议。

JsonSerializer could deal with this class.

My question is what classes generator is preferable to use and what are benefits and drawbacks of using each type of generated classes?
Thank you for your suggestions.

推荐答案

我假设你要反序列化JSON字符串到C#对象。我通常由我创建C#的对象,并使用 JsonConvert 反序列化JSON字符串。

I assume you want to deserialize json string to c# object. I usually create C# object by myself, and use JsonConvert to deserialize json string.

class Program {
        static void Main(string[] args)
        {
                string json = @"
                {
                        ""street_address"":""My street address"",
                        ""city"":""My City"",
                        ""state_province"":""My State Province"",
                        ""zip_postal_code"":""My Zip Postal Code"",
                }";

                Address address = JsonConvert.DeserializeObject<Address>(json);
                Console.WriteLine("Street address: {0}", address.StreetAddress);
                Console.WriteLine("City: {0}", address.City);
                Console.WriteLine("State province: {0}", address.StateProvince);
                Console.WriteLine("Zip postal code: {0}", address.ZipPostalCode);
        }
}

public class Address {
        [JsonProperty("street_address")]
        public string StreetAddress { get; set; }

        [JsonProperty("city")]
        public string City { get; set; }

        [JsonProperty("state_province")]
        public string StateProvince { get; set; }

        [JsonProperty("zip_postal_code")]
        public string ZipPostalCode { get; set; }
}

这篇关于福利和生成的C#类的缺点为JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆