• 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); 
    }
  • 相关阅读:
    Mat类具体解释(二)
    Android NDK开发篇(六):Java与原生代码通信(异常处理)
    Redis源代码剖析--对象object
    NioEventLoopGroup源码分析与线程设定
    零拷贝剖析以及用户空间与内核空间切换
    Java 字符集编码
    NIO网络编程
    NIO网络访问模式实践
    Spring Boot使用Html
    内存映射文件MappedByteBuffer和Buffer的Scattering与Gathering
  • 原文地址:https://www.cnblogs.com/hanglog/p/13743268.html
Copyright © 2020-2023  润新知