jonahxuworld 2021-12-08 14:40 采纳率: 93.1%
浏览 39
已结题

C#winform拿到的json数据如何让他自动循环生成我需要的呢?

这是我拿到的json,我想在listbox中显示processarr数组里的东西,比如13 ~ 3,13~ 2,13 -1,两两组合,
我要怎么写他能自动识别到有多少组?
而不用我手动一个一个取出来呢?
我现在是引用Newtonsoft.Json用的!
谢谢!

{
    "code": 1,
    "msg": "返回成功",
    "time": "1638940986",
    "data": {
        "workorder_id": 10,
        "work_order_no": "2111248",
        "product_id": 4,
        "process_num": 41,
        "template_address": "aa,
        "process_no_order": 3,
        "product_name": "我不知道  ",
        "exposure_time1": 20000,
        "exposure_time2": 17000,
        "min_score": 0.2,
        "standard": [{
            "standard_id": 6,
            "color": "黑色",
            "point_x": "-300",
            "point_y": "200",
            "L": 75.64,
            "a": 10,
            "b": 36.42
        }],
        "processarr": [{
            "process_no": "13",
            "process_no_order": 3
        }, {
            "process_no": 13,
            "process_no_order": 2
        }, {
            "process_no": 13,
            "process_no_order": 1
        }, {
            "process_no": 12,
            "process_no_order": 2
        }, {
            "process_no": 12,
            "process_no_order": 1
        }, {
            "process_no": 11,
            "process_no_order": 1
        }, {
            "process_no": 10,
            "process_no_order": 2
        }, {
            "process_no": 10,
            "process_no_order": 1
        }]
    }
}


  • 写回答

2条回答 默认 最新

  • 丨Haruna 2021-12-09 15:10
    关注

    创建实体类然后用JsonConvert.DeserializeObject方法。
    实体类可以直接用网上的JSON转C#实体类工具生成。

                string jsonString = "{\"code\": 1,\"msg\": \"返回成功\",\"time\": \"1638940986\",\"data\": {\"workorder_id\": 10,\"work_order_no\": \"2111248\",\"product_id\": 4,\"process_num\": 41,\"template_address\": \"aa\",\"process_no_order\": 3,\"product_name\": \"我不知道\",\"exposure_time1\": 20000,\"exposure_time2\": 17000,\"min_score\": 0.2,\"standard\": [{\"standard_id\": 6,\"color\": \"黑色\",\"point_x\": \"-300\",\"point_y\": \"200\",\"L\": 75.64,\"a\": 10,\"b\": 36.42}],\"processarr\": [{\"process_no\": \"13\",\"process_no_order\": 3}, {\"process_no\": 13,\"process_no_order\": 2}, {\"process_no\": 13,\"process_no_order\": 1}, {\"process_no\": 12,\"process_no_order\": 2}, {\"process_no\": 12,\"process_no_order\": 1}, {\"process_no\": 11,\"process_no_order\": 1}, {\"process_no\": 10,\"process_no_order\": 2}, {\"process_no\": 10,\"process_no_order\": 1}]}}";
                Root res = JsonConvert.DeserializeObject(jsonString,typeof(Root)) as Root;
                foreach(ProcessarrItem processarr in res.data.processarr)
                {
                    Console.WriteLine(processarr.process_no +"_"+ processarr.process_no_order);
                }
    
    
        public class StandardItem
        {
            public int standard_id { get; set; }
            public string color { get; set; }
            public string point_x { get; set; }
            public string point_y { get; set; }
            public double L { get; set; }
            public int a { get; set; }
            public double b { get; set; }
        }
    
        public class ProcessarrItem
        {
            public string process_no { get; set; }
            public int process_no_order { get; set; }
        }
    
        public class Data
        {
            public int workorder_id { get; set; }
            public string work_order_no { get; set; }
            public int product_id { get; set; }
            public int process_num { get; set; }
            public string template_address { get; set; }
            public int process_no_order { get; set; }
            public string product_name { get; set; }
            public int exposure_time1 { get; set; }
            public int exposure_time2 { get; set; }
            public double min_score { get; set; }
            public List<StandardItem> standard { get; set; }
            public List<ProcessarrItem> processarr { get; set; }
        }
    
        public class Root
        {
            public int code { get; set; }
            public string msg { get; set; }
            public string time { get; set; }
            public Data data { get; set; }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 修改了问题 12月8日
  • 创建了问题 12月8日

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp