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 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。