简体   繁体   English

Json反序列化C#

[英]Json Deserialize C#

I want to parsing this json file. 我想解析这个json文件。

{"features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[26.4217861898109,40.127607984644],[26.4219934821323,40.1275230229872],[26.4218810759267,40.1273800013679],[26.4216801413981,40.1274730404221],[26.4217861898109,40.127607984644]]]},"properties":{"ParselNo":"1","Alan":"340.48 m2","Mevkii":"-","Nitelik":"Arsa","Ada":"698","Il":"Çanakkale","Ilce":"Merkez","Pafta":"16","Mahalle":"Barbaros"}}],"type":"FeatureCollection","crs":{"type":"name","properties":{"name":"EPSG:4326"}}} { “特征”:[{ “类型”: “功能”, “几何”:{ “类型”: “多边形”, “坐标”:[[[26.4217861898109,40.127607984644],[26.4219934821323,40.1275230229872],[26.4218810759267,40.1273800013679 ],[26.4216801413981,40.1274730404221],[26.4217861898109,40.127607984644]]],“properties”:{“ParselNo”:“1”,“Alan”:“340.48 m2”,“Mevkii”:“ - ”,“Nitelik” : “ARSA”, “阿达”: “698”, “IL”: “恰纳卡莱”, “ILCE”: “MERKEZ”, “Pafta”: “16”, “Mahalle”: “巴尔巴罗斯”}}],“类型“:” 的FeatureCollection”, “CRS”:{ “类型”: “姓名”, “属性”:{ “名称”: “EPSG:4326”}}}

I try this code: 我试试这段代码:

string fileName = file.FileName;
string json = System.IO.File.ReadAllText(fileName);
dynamic stuff = JsonConvert.DeserializeObject(json);

string s1 = Convert.ToString(stuff.features.Count);
string s2 = stuff.properties.ParselNo;
MessageBox.Show(s1);
MessageBox.Show(s2);

s1 is working but s2 give error. s1正在工作,但s2给出了错误。 How can I fix it? 我该如何解决?

I want to get it like this: 我希望得到这样的:

Coordinates[] coordinates = [(26.xx, 40.xx), (26.xx,40.xx) ... ]
ParselNo = 1
Alan = 340.48
Nitelik = Arsa
Ada = 698
İl = Çanakkale
İlçe = Merkez
Pafta = 16
dat_name = EPSG:4326

what can i do? 我能做什么?

If you properly format your json as below i believe 如果你正确格式化你的json,我相信

 stuff.features.properties.ParselNo;

should get the the Parse1No 应该得到Parse1No

  {  
   "features":[  
      {  
         "type":"Feature",
         "geometry":{  
            "type":"Polygon",
            "coordinates":[  
               [  
                  [  
                     26.4217861898109,
                     40.127607984644
                  ],
                  [  
                     26.4219934821323,
                     40.1275230229872
                  ],
                  [  
                     26.4218810759267,
                     40.1273800013679
                  ],
                  [  
                     26.4216801413981,
                     40.1274730404221
                  ],
                  [  
                     26.4217861898109,
                     40.127607984644
                  ]
               ]
            ]
         },
         "properties":{  
            "ParselNo":"1",
            "Alan":"340.48 m2",
            "Mevkii":"-",
            "Nitelik":"Arsa",
            "Ada":"698",
            "Il":"Çanakkale",
            "Ilce":"Merkez",
            "Pafta":"16",
            "Mahalle":"Barbaros"
         }
      }
   ],
   "type":"FeatureCollection",
   "crs":{  
      "type":"name",
      "properties":{  
         "name":"EPSG:4326"
      }
   }
}

According to the json you provided features are List<Feature> so you cannot access a property of list like stuff.features.properties.ParselNo it should be 根据json你提供的功能是List<Feature>所以你不能访问像stuff.features.properties.ParselNo这样的列表属性它应该是

stuff.features[0].properties.ParselNo

i used features[0] because it will get the first feature from the list and features.Count will return the number of features in list you can use http://json2csharp.com/ to check your json 我使用的features[0]因为它会得到第一个feature从列表中features.Count将返回的数量featureslist ,你可以使用http://json2csharp.com/检查您的json

Edit 编辑

and about your second question it should be like this 关于你的第二个问题应该是这样的

List<List<double>> s2 =stuff.features[0].geometry.coordinates[0].ToObject<List<List<double>>>();
var firstcordinates = s2[0];

it will get the coordinates from array and s2[0] will get first coordinates from list 它将从数组中获取坐标,而s2[0]将从列表中获取第一个坐标

I try this: 我试试这个:

 string s2 = stuff.features.properties.ParselNo;
 MessageBox.Show(s2);

give this error: Error... 给出这个错误: 错误......

This code work: stuff.features[0].properties.ParselNo But don't this: stuff.features[0].geometry.coordinates[0]; 这段代码工作:stuff.features [0] .properties.ParselNo但不要这样:stuff.features [0] .geometry.coordinates [0];

I don't understand it :( Error code.. 我不明白:( 错误代码..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM