Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于JSON结构正确定义.NET类_C#_.net_Json_Class - Fatal编程技术网

C# 基于JSON结构正确定义.NET类

C# 基于JSON结构正确定义.NET类,c#,.net,json,class,C#,.net,Json,Class,我正在试图了解在.NET3.5中建立一个类的最佳方法。我有一个JSON对象,它是订单发货的集合,定义如下: [ { "BaseOrder": { "order_id": "503", "BillingAddress": { "bill_address_id": "984", "order_id": "503" },

我正在试图了解在.NET3.5中建立一个类的最佳方法。我有一个JSON对象,它是订单发货的集合,定义如下:

[
    {
        "BaseOrder": {
            "order_id": "503",
            "BillingAddress": {
                "bill_address_id": "984",
                "order_id": "503"
            },              
            "Lookup": [
                {
                    "lookup_id": "4070",
                    "order_id": "503"   
                },
                {
                    "lookup_id": "4071",
                    "order_id": "503"   
                }
            ],              
            "ShippingAddress": {
                "ship_address_id": "983",
                "order_id": "503"
            }
        },
        "BaseOrderShipment": {
            "shipment_id": "535",
            "order_id": "503"           
        },
        "BaseOrderShipmentLineitem": [
            {
                "line_item_id": "820",          
                "order_id": "503",
                "shipment_id": "535"
            },
            {
                "line_item_id": "821",          
                "order_id": "503",
                "shipment_id": "535"
            }
        ]
    },
    {
        "BaseOrder": {
            "order_id": "5030",
            "BillingAddress": {
                "bill_address_id": "9840",
                "order_id": "5030"
            },              
            "Lookup": [
                {
                    "lookup_id": "40700",
                    "order_id": "5030"
                },
                {
                    "lookup_id": "40710",   
                    "order_id": "5030"  
                }
            ],  
            "ShippingAddress": {
                "ship_address_id": "9830",
                "order_id": "5030"
            }
        },
        "BaseOrderShipment": {
            "shipment_id": "5350",
            "order_id": "5030"          
        },
        "BaseOrderShipmentLineitem": [
            {
                "line_item_id": "8200",         
                "order_id": "5030",
                "shipment_id": "5350"
            }
        ]
    }
]
我不太确定该如何安排这个班。我计划把这个类放在它自己的文件中,并从我的程序中引用它

这就是我计划放在类文件中的内容:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace MyCompany.MyProgram
{

public class RootObject
{
    [JsonProperty("BaseOrder")]
    public BaseOrder BaseOrder { get; set; }

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

    [JsonProperty("BaseOrderShipmentLineitem")]
    public IList<BaseOrderShipmentLineitem> BaseOrderShipmentLineitem { get; set; }
}

public class BaseOrders
{
    [JsonProperty("order_id")]
    public int OrderId { get; set; }

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

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

    [JsonProperty("Lookup")]
    public IList<Lookup> Lookup { get; set; }
}

public class BaseOrderShipment
{
    [JsonProperty("shipment_id")]
    public int ShipmentId { get; set; }

    [JsonProperty("order_id")]
    public int OrderId { get; set; }
}

public class BillingAddress
{
    [JsonProperty("bill_address_id")]
    public int BillAddressId { get; set; }

    [JsonProperty("order_id")]
    public int OrderId { get; set; }
}

public class ShippingAddress
{
    [JsonProperty("ship_address_id")]
    public int ShipAddressId { get; set; }

    [JsonProperty("order_id")]
    public int OrderId { get; set; }
}

public class Lookup
{
    [JsonProperty("lookup_id")]
    public int LookupId { get; set; }

    [JsonProperty("order_id")]
    public int OrderId { get; set; }
}

public class BaseOrderShipmentLineitem
{
    [JsonProperty("line_item_id")]
    public int LineItemId { get; set; }

    [JsonProperty("order_id")]
    public int OrderId { get; set; }

    [JsonProperty("shipment_id")]
    public int ShipmentId { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
名称空间MyCompany.MyProgram
{
公共类根对象
{
[JsonProperty(“BaseOrder”)]
公共基序基序{get;set;}
[JsonProperty(“BaseOrderShipping”)]
public BaseOrderShipping BaseOrderShipping{get;set;}
[JsonProperty(“BaseOrderShipmentLineitem”)]
公共IList BaseOrderShipmentLineitem{get;set;}
}
公共类基序
{
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
[JsonProperty(“发货地址”)]
公共ShippingAddress ShippingAddress{get;set;}
[JsonProperty(“BillingAddress”)]
公共计费地址计费地址{get;set;}
[JsonProperty(“查找”)]
公共IList查找{get;set;}
}
公共类BaseOrderShipping
{
[JsonProperty(“装运id”)]
public int ShipmentId{get;set;}
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
}
公共类计费地址
{
[JsonProperty(“账单地址”)]
public int BillAddressId{get;set;}
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
}
公共类发货地址
{
[JsonProperty(“发货地址id”)]
public int ShipAddressId{get;set;}
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
}
公共类查找
{
[JsonProperty(“查找id”)]
public int LookupId{get;set;}
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
}
公共类BaseOrderShipmentLineitem
{
[JsonProperty(“行项目id”)]
public int LineItemId{get;set;}
[JsonProperty(“订单id”)]
公共int-OrderId{get;set;}
[JsonProperty(“装运id”)]
public int ShipmentId{get;set;}
}
我正在使用以下命令创建该类的实例:

string fileName = @"C:\path\to\myFile.json";
var obj = ParseOrderShipments(fileName);
public static List<RootObject> ParseOrderShipments(string fileName)
{
    //RootObject retObj = new RootObject();
    List<RootObject> retObj = JsonConvert.DeserializeObject<List<RootObject>>(File.ReadAllText(fileName));
    return retObj;
}
stringfilename=@“C:\path\to\myFile.json”;
var obj=parseOrderShippings(文件名);
公共静态列表ParseOrderShippings(字符串文件名)
{
//RootObject retObj=新的RootObject();
List retObj=JsonConvert.DeserializeObject(File.ReadAllText(文件名));
返回retObj;
}

这是一个很好的方法,还是有更好的方法呢?

IMHO这是一个很好的JSON到C#的翻译,但你这里的问题是JSON结构本身:它看起来有很多冗余字段(例如,
order_id
重复了很多次),一般来说,它似乎是直接从数据库中提取出来的,也就是说,它使用的是关系模型,有大量ID引用其他“表”,而不是面向对象的模型


有句名言说“计算机科学中的所有问题都可以通过另一种间接方式来解决”;根据您希望对这些数据执行的操作,您可能希望构建另一组以更面向对象的方式表示订单的类,例如,作为装运属性的行项目。

为什么不使用类似生成器的

以下是它的输出:

public class BillingAddress
{
    public string bill_address_id { get; set; }
    public string order_id { get; set; }
}

public class Lookup
{
    public string lookup_id { get; set; }
    public string order_id { get; set; }
}

public class ShippingAddress
{
    public string ship_address_id { get; set; }
    public string order_id { get; set; }
}

public class BaseOrder
{
    public string order_id { get; set; }
    public BillingAddress BillingAddress { get; set; }
    public List<Lookup> Lookup { get; set; }
    public ShippingAddress ShippingAddress { get; set; }
}

public class BaseOrderShipment
{
    public string shipment_id { get; set; }
    public string order_id { get; set; }
}

public class BaseOrderShipmentLineitem
{
    public string line_item_id { get; set; }
    public string order_id { get; set; }
    public string shipment_id { get; set; }
}

public class RootObject
{
    public BaseOrder BaseOrder { get; set; }
    public BaseOrderShipment BaseOrderShipment { get; set; }
    public List<BaseOrderShipmentLineitem> BaseOrderShipmentLineitem { get; set; }
}
公共类计费地址
{
公共字符串bill\u address\u id{get;set;}
公共字符串顺序_id{get;set;}
}
公共类查找
{
公共字符串查找\u id{get;set;}
公共字符串顺序_id{get;set;}
}
公共类发货地址
{
公共字符串ship_address_id{get;set;}
公共字符串顺序_id{get;set;}
}
公共类基本秩序
{
公共字符串顺序_id{get;set;}
公共计费地址计费地址{get;set;}
公共列表查找{get;set;}
公共ShippingAddress ShippingAddress{get;set;}
}
公共类BaseOrderShipping
{
公共字符串shipping_id{get;set;}
公共字符串顺序_id{get;set;}
}
公共类BaseOrderShipmentLineitem
{
公共字符串行\项\ id{get;set;}
公共字符串顺序_id{get;set;}
公共字符串shipping_id{get;set;}
}
公共类根对象
{
公共基序基序{get;set;}
public BaseOrderShipping BaseOrderShipping{get;set;}
公共列表BaseOrderShipmentLineitem{get;set;}
}

稍后可以使用
JsonConvert.DeserializeObject(jsonString)反序列化字符串;

您使用的是什么JSON序列化引擎?您可能不需要大部分的
JsonProperty
属性,因为属性名称与JSON元素匹配names@jared:查看此链接以从JSON生成C#classess。@Konamiman根据我的经验,您不应该依赖属性名。请显式绑定,然后当然,你不必担心重构会毁了你的一天。@Gusdor是真的,但如果你只把它们作为DTO使用,你应该意识到更改它们会导致问题——无论你在哪里都肯定会有一个测试套件来通知你这一事实;)-剩下的就是YAGNI/KISSIMO@Konamimandie引擎似乎是JSON.net(基于导入)这些类比OP的建议糟糕得多;它们不遵守C#命名约定,如果名称被重构,它们将被破坏,当int足够大时,它们会使用字符串。这只是一个可以节省大量键入的建议。您以后可以随时添加属性。通过查看json,您如何猜测什么是int和什么是a字符串order\u id在数据库中很可能定义为字符串。如果您必须根据某些业务逻辑验证json,该怎么办?例如,您正在编写web api,有人向您发布json和order\u id丢失或其他内容。您如何生成错误响应,指出order\u id必须是有效的整数他们是否提供了“order1”作为值?这取决于开发人员想要什么