首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将多个json反序列化为对象c#

将多个json反序列化为对象c#
EN

Stack Overflow用户
提问于 2018-02-02 14:41:44
回答 2查看 10.9K关注 0票数 1

我试图用多个对象反序列化API调用中的json字符串,但没有成功。

杰森:

代码语言:javascript
运行
复制
@{
    "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
        }]

我创建了以下模型类:

代码语言:javascript
运行
复制
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来做到这一点。

代码语言:javascript
运行
复制
(purchaseOrder purchaseOrderobject  = JsonConvert.DeserializeObject<purchaseOrder >(json);)

但没有成功。我想也许我得用字典来做这件事,但我不太清楚该怎么做。

是否有一种方法可以通过一个接一个地获取json对象,然后像下面的链接那样反序列化它们呢?

反序列化单个对象

EN

Stack Overflow用户

回答已采纳

发布于 2018-02-02 14:45:13

你说“没有成功”,所以不清楚是否有错误或什么。但是..。

我认为这个问题是你的根本目标。我在json2csharp.com中运行了您的JSON,这就是它得出的结果:

代码语言:javascript
运行
复制
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);试试吧

票数 5
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48585029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档