複雜json數據怎麼轉爲實體對象

  1 //json數據 result
  2 
  3 string result="
  4 
  5 {
  6 "img_height": 533, 
  7 "img_id": "ff4c286cf9039dcd0a5cfc244e28edae", 
  8 "img_width": 800, 
  9 "result": {
 10 "pedestrian": [
 11 {
 12 "confidence": 0.9987247979263988, 
 13 "position": {
 14 "center": {
 15 "x": 0.454688, 
 16 "y": 0.896714
 17 }, 
 18 "size": {
 19 "height": 0.117371, 
 20 "width": 0.03125
 21 }, 
 22 "type": "RECT"
 23 }
 24 }
 25 ], 
 26 "scene": [
 27 {
 28 "attribute": {
 29 "tag": [
 30 {
 31 "confidence": 0.746287, 
 32 "value": "Cornfield"
 33 }
 34 ]
 35 }, 
 36 "position": {
 37 "type": "NA"
 38 }
 39 }
 40 ], 
 41 "text": [
 42 {
 43 "attribute": {
 44 "content": {
 45 "confidence": 0.993998, 
 46 "value": "\u5c81\u6708"
 47 }
 48 }, 
 49 "confidence": 0.810002, 
 50 "position": {
 51 "point": [
 52 {
 53 "x": 0.481583, 
 54 "y": 0.781784
 55 }, 
 56 {
 57 "x": 0.483448, 
 58 "y": 0.699125
 59 }, 
 60 {
 61 "x": 0.587641, 
 62 "y": 0.704432
 63 }, 
 64 {
 65 "x": 0.585777, 
 66 "y": 0.787091
 67 }
 68 ], 
 69 "type": "POLYGON"
 70 }
 71 }, 
 72 {
 73 "attribute": {
 74 "content": {
 75 "confidence": 0.831358, 
 76 "value": "\u5f20\u7167\u5802"
 77 }
 78 }, 
 79 "confidence": 0.995801, 
 80 "position": {
 81 "point": [
 82 {
 83 "x": 0.263567, 
 84 "y": 0.780145
 85 }, 
 86 {
 87 "x": 0.263539, 
 88 "y": 0.70621
 89 }, 
 90 {
 91 "x": 0.422004, 
 92 "y": 0.706071
 93 }, 
 94 {
 95 "x": 0.422033, 
 96 "y": 0.780006
 97 }
 98 ], 
 99 "type": "POLYGON"
100 }
101 }
102 ]
103 }";
104 
105 //利用 Newtonsoft.dll 中的JsonConvert調用
106 
107 var obj = JsonConvert.DeserializeObject<ResultJson>(result);
108 string message = obj.result.text.FirstOrDefault().attribute.content.value;
json數據以及轉換方法調用
 1 public class ResultJson
 2     {
 3         public string img_height { set; get; }
 4         public string img_id { set; get; }
 5         public string img_width { set; get; }
 6         public Result result { set; get; }
 7     }
 8     public class Result
 9     {
10         public List<Scene> scene { set; get; }
11         public List<RText> text { set; get; }
12         public List<SPedestrian> pedestrian { get; set; }
13     }
14     public class Scene
15     {
16         public SAttribute attribute { set; get; }
17         public SPosition position { set; get; }
18     }
19     public class SPedestrian
20     {
21         public string confidence { get; set; }
22         public PPosition position { get; set; }
23     }
24     public class PPosition
25     {
26         public PCenter center { get; set; }
27         public PSize size { get; set; }
28         public string type { get; set; }
29     }
30     public class PCenter
31     {
32         public string x { get; set; }
33         public string y { get; set; }
34     }
35     public class PSize
36     {
37         public string height { get; set; }
38         public string wight { get; set; }
39     }
40 
41     public class SAttribute
42     {
43         public List<Tag> tag { set; get; }
44     }
45     public class Tag
46     {
47         public string confidence { set; get; }
48         public string value { set; get; }
49     }
50     public class SPosition
51     {
52         public string type { set; get; }
53     }
54     public class RText
55     {
56         public TAttribute attribute { set; get; }
57         public string confidence { set; get; }
58         public TPosition position { set; get; }
59     }
60     public class TAttribute
61     {
62         public TContent content { set; get; }
63     }
64     public class TContent
65     {
66         public string confidence { set; get; }
67         public string value { set; get; }
68     }
69 
70     public class TPosition
71     {
72         public List<TPoint> point { set; get; }
73         public string type { set; get; }
74     }
75     public class TPoint
76     {
77         public string x { set; get; }
78         public string y { set; get; }
79     }
80     #endregion
JSON實體類

 

 

以上就是代碼,不明白的可以問我。

總結,json中凡是[]的都用List集合,而{}的都是用字段。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章