json格式化为c#类,以集合方式处理数据

本文介绍了一个具体的案例,展示了如何将JSON数据转换为C#类,并提供了详细的类定义示例。通过解析JSON数据,可以获取人物属性和位置信息,最后以集合方式访问这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

json数据:

{
  "person_num": 1,
  "person_info": [
    {
      "attributes": {
        "upper_wear_fg": {
          "score": 0.97389006614685059,
          "name": "T恤"
        },
        "cellphone": {
          "score": 0.99958902597427368,
          "name": "未使用手机"
        },
        "lower_cut": {
          "score": 0.98942846059799194,
          "name": "有下方截断"
        },
        "umbrella": {
          "score": 0.999944806098938,
          "name": "未打伞"
        },
        "orientation": {
          "score": 0.99943989515304565,
          "name": "正面"
        },
        "is_human": {
          "score": 0.96589845418930054,
          "name": "不确定"
        },
        "headwear": {
          "score": 0.99876141548156738,
          "name": "无帽"
        },
        "gender": {
          "score": 0.68398737907409668,
          "name": "男性"
        },
        "age": {
          "score": 0.60736602544784546,
          "name": "中年"
        },
        "upper_cut": {
          "score": 0.99997174739837646,
          "name": "无上方截断"
        },
        "glasses": {
          "score": 0.99150675535202026,
          "name": "无眼镜"
        },
        "lower_color": {
          "score": 0.69293707609176636,
          "name": "不确定"
        },
        "bag": {
          "score": 0.97373586893081665,
          "name": "无背包"
        },
        "upper_wear_texture": {
          "score": 0.86429083347320557,
          "name": "图案"
        },
        "smoke": {
          "score": 0.99937230348587036,
          "name": "未吸烟"
        },
        "vehicle": {
          "score": 0.99966096878051758,
          "name": "无交通工具"
        },
        "lower_wear": {
          "score": 0.98588055372238159,
          "name": "不确定"
        },
        "carrying_item": {
          "score": 0.684053897857666,
          "name": "无手提物"
        },
        "upper_wear": {
          "score": 0.99992609024047852,
          "name": "短袖"
        },
        "occlusion": {
          "score": 0.98508137464523315,
          "name": "无遮挡"
        },
        "upper_color": {
          "score": 0.99971216917037964,
          "name": "蓝"
        }
      },
      "location": {
        "height": 286,
        "width": 202,
        "top": 134,
        "score": 0.98026180267333984,
        "left": 297
      }
    },
  "log_id": 4793816596900492591
}

json格式化c#类定义:

public class Upper_wear_fg {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Cellphone {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_cut {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Umbrella {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Orientation {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Is_human {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Headwear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Gender {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Age {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_cut {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Glasses {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_color {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Bag {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_wear_texture {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Smoke {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Vehicle {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_wear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Carrying_item {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_wear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Occlusion {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_color {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Attributes {
	public Upper_wear_fg upper_wear_fg { get; set; }
	public Cellphone cellphone { get; set; }
	public Lower_cut lower_cut { get; set; }
	public Umbrella umbrella { get; set; }
	public Orientation orientation { get; set; }
	public Is_human is_human { get; set; }
	public Headwear headwear { get; set; }
	public Gender gender { get; set; }
	public Age age { get; set; }
	public Upper_cut upper_cut { get; set; }
	public Glasses glasses { get; set; }
	public Lower_color lower_color { get; set; }
	public Bag bag { get; set; }
	public Upper_wear_texture upper_wear_texture { get; set; }
	public Smoke smoke { get; set; }
	public Vehicle vehicle { get; set; }
	public Lower_wear lower_wear { get; set; }
	public Carrying_item carrying_item { get; set; }
	public Upper_wear upper_wear { get; set; }
	public Occlusion occlusion { get; set; }
	public Upper_color upper_color { get; set; }
}

public class Location {
	public string height  { get; set; }
	public string width  { get; set; }
	public string top  { get; set; }
	public string score  { get; set; }
	public string left  { get; set; }
}

public class Person_info {
	public Attributes attributes { get; set; }
	public Location location { get; set; }
}

public class RootObject {
	public string person_num  { get; set; }
	public List<Person_info> person_info { get; set; }
	public string log_id  { get; set; }
}

json数据处理:

RootObject rb = JsonConvert.DeserializeObject<RootObject>(result);
List<Person_info> person_info = rb.person_info;

最后以集合的方式访问即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ya土豆儿~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值