简体   繁体   中英

Json Deserialize C#

I want to parsing this json file.

{"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"}}}

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. 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

 stuff.features.properties.ParselNo;

should get the the 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

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

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

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];

I don't understand it :( Error code..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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