将多个 JSON 反序列化为 C# 对象

3
我可以为您提供中文翻译服务,这段内容是关于编程的。您需要反序列化来自API调用的json字符串,但没有获得很大的成功。
JSON:
@{
    "purchaseOrders": [
        {
            "supplierId": "500",
            "currencyCode": "EUR",
            "companyId": "LALA",
            "companyName": "LALA",
            "purchaseOrderLines": [
                {
                    "lineNumber": "10",
                    "itemNumber": "255",
                    "itemDescription": "TestItem2",
                    "unitPrice": 24.64,
                    "quantity": 2,
                    "isServiceBased": false,
                    "taxIndicator1": "LAAA5",
                    "taxIndicator2": "4",
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                },
                {
                    "lineNumber": "20",
                    "itemNumber": "5555555",
                    "itemDescription": "3test, Ind",
                    "unitPrice": 32.56,
                    "quantity": 2,
                    "isServiceBased": false,
                    "taxIndicator1": "LAAA5",
                    "taxIndicator2": "4",
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                }
            ],
            "orderIdentifier": "261656",
            "supplierName": "Lopes BVBA",
            "orderType": "T",
            "isActive": true
        },
        {
            "supplierId": "5555",
            "currencyCode": "EUR",
            "companyId": "API",
            "companyName": "LALA2",
            "purchaseOrderLines": [
                {
                    "lineNumber": "1",
                    "itemNumber": "448",
                    "itemDescription": "TestItem",
                    "unitPrice": 1563.23117,
                    "quantity": 1,
                    "isServiceBased": false,
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                },
                {
                    "lineNumber": "2",
                    "itemNumber": "5551",
                    "itemDescription": "Test",
                    "unitPrice": 524.92539,
                    "quantity": 1,
                    "isServiceBased": false,
                    "unit": "-",
                    "deliveryLines": [],
                    "supplierItems": [],
                    "isActive": true
                }
            ],
            "orderIdentifier": "84615",
            "supplierName": "CLopes.",
            "orderType": "T",
            "isActive": true
        }]

我创建了以下模型类:

public class purchaseOrder
{
        public string supplierId { get; set; }
        public string currencyCode { get; set; }
        public string companyId { get; set; }
        public string companyName { get; set; }
        public List<purchaseOrderLines> purchaseOrderLines { get; set; }
        public double orderIdentifier { get; set; }
        public string supplierName { get; set; }
        public string Ordertype { get; set; }
        public bool isActive { get; set; }
}

public class purchaseOrderLines
{
        public int lineNumber { get; set; }
        public string itemnumber { get; set; }
        public string itemDescription { get; set; }
        public double unitPrice { get; set; }
        public int quantity { get; set; }
        public bool isServiceBased { get; set; }
        public string unit { get; set; }  
        public List<deliveryLines> deliveryLines { get; set; }
        public string[] supplierItems { get; set; }
        public bool isActive { get; set; }
}

public class deliveryLines
{
        public int deliveredQuantity { get; set; }
        public DateTime? deliveredDate { get; set; }
        public string   deliveryNote { get; set; }
        public bool isActive { get; set; }
}

我尝试通过将字符串反序列化为purchaseOrder来实现此操作

(purchaseOrder purchaseOrderobject  = JsonConvert.DeserializeObject<purchaseOrder >(json);)

但是没有成功。我认为可能需要使用字典,但我不确定如何做。

有没有一种方法可以逐个获取json对象并像以下链接中的方式反序列化它们?

反序列化单个对象


“no success” 的意思是什么?同时,你想逐个获取哪些 Json 对象并且为什么或者如何获取也不是很清楚。 - undefined
你已经有一个字符串(不是流),所以将其转换为1个purchaseOrder并使用它没有明显的问题。 - undefined
2个回答

6
您说“没有成功”,所以不清楚是否存在错误。但是......
我认为问题在于您的根对象。我通过json2csharp.com运行了您的JSON,以下是它生成的代码:
public class PurchaseOrderLine
{
    public string lineNumber { get; set; }
    public string itemNumber { get; set; }
    public string itemDescription { get; set; }
    public double unitPrice { get; set; }
    public int quantity { get; set; }
    public bool isServiceBased { get; set; }
    public string taxIndicator1 { get; set; }
    public string taxIndicator2 { get; set; }
    public string unit { get; set; }
    public List<object> deliveryLines { get; set; }
    public List<object> supplierItems { get; set; }
    public bool isActive { get; set; }
}

public class PurchaseOrder
{
    public string supplierId { get; set; }
    public string currencyCode { get; set; }
    public string companyId { get; set; }
    public string companyName { get; set; }
    public List<PurchaseOrderLine> purchaseOrderLines { get; set; }
    public string orderIdentifier { get; set; }
    public string supplierName { get; set; }
    public string orderType { get; set; }
    public bool isActive { get; set; }
}

public class RootObject
{
    public List<PurchaseOrder> purchaseOrders { get; set; }
}

尝试使用以下代码:var root = JsonConvert.DeserializeObject<RootObject>(json);

1
这个做法有效。计数似乎是200,不是我预期的201,但我可以接受。谢谢!在可以的时候,我会批准回答。 - undefined

3
你的JSON是一个采购订单数组,因此你需要将其反序列化为列表。
var list = JsonConvert.DeserializeObject<List<purchaseOrder>>(json);)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接