c# 多层JSON数据动态解析

1.json数据

{
    "workOrderId": "WW0000001-01-01",
    "orderDate": "2020-01-01",
    "productNum": "1",
    "productId": "P1205000900",
    "productModel": "TRD-271000AF",
    "planModel": "P",
    "productSimpleCode": "P1205",
    "productQuantity": 100,
    "workshop": "",
    "workCore": "",
    "procedure": [{
            "orderId": "PG0001",
            "procedureId": "01",
            "procedureName": "插件焊接",
            "id": 1,
            "materialInfo": [{
                "materialId": "C001",
                "positionNum": "",
                "trayCode": "tp001-1",
                "singleUse": "1",
                "materialUse": "100"
            }, {
                "materialId": "C002",
                "positionNum": "",
                "trayCode": "tp001-2",
                "singleUse": "2",
                "materialUse": "50"
            }]
        }, {
            "orderId": "PG0002",
            "procedureId": "02",
            "procedureName": "LED安装",
            "id": 2,
            "materialInfo": [{
                "materialId": "C003",
                "positionNum": "",
                "trayCode": "tp002-1",
                "singleUse": "1",
                "materialUse": "100"
            }, {
                "materialId": "C004",
                "positionNum": "",
                "trayCode": "tp002-2",
                "singleUse": "2",
                "materialUse": "50"
            }]
        }, {
            "orderId": "PG0003",
            "procedureId": "03",
            "procedureName": "光栅贴附",
            "id": 3,
            "materialInfo": [{
                "materialId": "C004",
                "positionNum": "",
                "trayCode": "tp003-1",
                "singleUse": "1",
                "materialUse": "100"
            }, {
                "materialId": "C005",
                "positionNum": "",
                "trayCode": "tp003-2",
                "singleUse": "2",
                "materialUse": "50"
            }]
        }]
}

 

2.json对象创建(注:对象名称必须与json数据名称保持一致)

using System.Collections.Generic;

namespace Global
{
    public class WorkOrderReceived
    {
        public string workOrderId { get; set; }

        public string orderDate { get; set; }

        public string productNum { get; set; }

        public string productId { get; set; }

        public string productModel { get; set; }

        public string planModel { get; set; }

        public string productSimpleCode { get; set; }

        public string productQuantity { get; set; }

        public string workshop { get; set; }

        public string workCore { get; set; }

        public List<procedure> procedure { get; set; }

    }

    public class procedure
    {
        public string orderId { get; set; }

        public string procedureId { get; set; }

        public string procedureName { get; set; }

        public string id { get; set; }

        public List<materialInfo> materialInfo { get; set; } 
    }

    public class materialInfo
    {
        public string materialId { get; set; }

        public string positionNum { get; set; }

        public string trayCode { get; set; }

        public string singleUse { get; set; }

        public string materialUse { get; set; }
    }
}

3.解析代码

using Newtonsoft.Json;   
、
 public void AnaysisJson(string jsonValue)
 {
     WorkOrderReceived workOrderReceived = JsonConvert.DeserializeObject<WorkOrderReceived>(jsonValue); 
}

 

posted @ 2020-09-28 09:54  沉浮。。。  阅读(2027)  评论(0编辑  收藏  举报