jonahxuworld
2021-12-08 14:40
采纳率: 95.7%
浏览 34

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条回答 默认 最新

  • Jiachen Li-MSFT 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条)

相关推荐 更多相似问题