json数据格式是:
{
"face": [
{
"attribute": {
"age": {
"range": 5,
"value": 23
},
"gender": {
"confidence": 99.9999,
"value": "Female"
},
"glass": {
"confidence": 99.945,
"value": "None"
},
"pose": {
"pitch_angle": {
"value": 17
},
"roll_angle": {
"value": 0.735735
},
"yaw_angle": {
"value": -2
}
},
"race": {
"confidence": 99.6121,
"value": "Asian"
},
"smiling": {
"value": 4.86501
}
},
"face_id": "17233b4b1b51ac91e391e5afe130eb78",
"position": {
"center": {
"x": 49.4,
"y": 37.6
},
"eye_left": {
"x": 43.3692,
"y": 30.8192
},
"eye_right": {
"x": 56.5606,
"y": 30.9886
},
"height": 26.8,
"mouth_left": {
"x": 46.1326,
"y": 44.9468
},
"mouth_right": {
"x": 54.2592,
"y": 44.6282
},
"nose": {
"x": 49.9404,
"y": 38.8484
},
"width": 26.8
},
"tag": ""
}
],
"img_height": 500,
"img_id": "22fd9efc64c87e00224c33dd8718eec7",
"img_width": 500,
"session_id": "38047ad0f0b34c7e8c6efb6ba39ed355",
"url": "http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/1.jpg?v=4"
}
这个数据被我存在了textBox4中,
然后我用的方法是
List<Data1> infoList = JsonConvert.DeserializeObject<List<Data1>>(textBox4.Text);
infoList.ForEach(x => x.face.ForEach(y => y.attribute.ForEach(z=>z.age.ForEach(w=>Console.WriteLine(w.value)))));
}
public class Data1 {
public List<Data2>face { get; set; }
}
public class Data2 {
public List<Data3> attribute { get; set; }
}
public class Data3 {
public List<Data4> age { get; set; }
}
public class Data4 {
public string range { get; set; }
public string value { get; set; }
}
但是会报错
请问是什么问题呢?怎么解决?求大神!!!
------解决思路----------------------
1、定义类
public class Age {
public string range { get; set; }
public string value { get; set; }
}
public class Gender {
public string confidence { get; set; }
public string value { get; set; }
}
public class Glass {
public string confidence { get; set; }
public string value { get; set; }
}
public class Pitch_angle {
public string value { get; set; }
}
public class Roll_angle {
public string value { get; set; }
}
public class Yaw_angle {
public string value { get; set; }
}
public class Pose {
public Pitch_angle pitch_angle { get; set; }
public Roll_angle roll_angle { get; set; }
public Yaw_angle yaw_angle { get; set; }
}
public class Race {
public string confidence { get; set; }
public string value { get; set; }
}
public class Smiling {
public string value { get; set; }
}
public class Attribute {
public Age age { get; set; }
public Gender gender { get; set; }
public Glass glass { get; set; }
public Pose pose { get; set; }
public Race race { get; set; }
public Smiling smiling { get; set; }
}
public class Center {
public string x { get; set; }
public string y { get; set; }
}
public class Eye_left {
public string x { get; set; }
public string y { get; set; }
}
public class Eye_right {
public string x { get; set; }
public string y { get; set; }
}
public class Mouth_left {
public string x { get; set; }
public string y { get; set; }
}
public class Mouth_right {
public string x { get; set; }
public string y { get; set; }
}
public class Nose {
public string x { get; set; }
public string y { get; set; }
}
public class Position {
public Center center { get; set; }
public Eye_left eye_left { get; set; }
public Eye_right eye_right { get; set; }
public string height { get; set; }
public Mouth_left mouth_left { get; set; }
public Mouth_right mouth_right { get; set; }
public Nose nose { get; set; }
public string width { get; set; }
}
public class Face {
public Attribute attribute { get; set; }
public string face_id { get; set; }
public Position position { get; set; }
public string tag { get; set; }
}
public class RootObject {
public List<Face> face { get; set; }
public string img_height { get; set; }
public string img_id { get; set; }
public string img_width { get; set; }
public string session_id { get; set; }
public string url { get; set; }
}
2、使用Newtonsoft.Json进行解析
RootObject ro = JsonConvert.DeserializeObject<RootObject>(Json字符串);