将Json Response反序列化为对象 [英] Deserializing Json Response into object

查看:74
本文介绍了将Json Response反序列化为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用CloudMade Api来使用地图。这个Api返回Json结果如下:



{places:[{addressType:street,city:~Slough, country:United Kingdom,featureType:Ortsstrasse,position:{lat:51.51465939,lon: - 0.58932440},州:英格兰,街道:Adrians Walk ,zip:SL2 5AT},{addressType:视线,城市:〜斯劳,国家:英国,featureType:建立感兴趣的地形 ,名称:斯劳,位置:{lat:51.51206867,lon: - 0.59121503},州:英格兰,街道:铁路台},{addressType :视线,城市:〜斯劳,国家:英国,featureType:站,名称:斯劳,位置:{lat:51.51225777, lon: - 0.59193386},state:England,street:Railway Terrace}],status:{duration:636,procedure:geo.location.search.2 ,成功:真实}}



我想将此响应反序列化为对象。我目前正在使用以下对象。

但它抛出错误无法将JSON对象反序列化为类型'OpenstreetMap.places []'。



公共课堂地方

{

public string addressType {get;组; }

public string city {get;组; }

public string country {get;组; }

public string featureType {get;组; }

public List< object>位置{get;组; }

公共字符串状态{get;组; }

public string street {get;组; }

public string zip {get;组; }

public List< object> status {get;组; }

}



我在下面使用代码:

Hi All,

I am using CloudMade Api for using maps. This Api is returning Json result as Below:

{"places":[{"addressType":"street","city":"~Slough","country":"United Kingdom","featureType":"Ortsstrasse","position":{"lat":51.51465939,"lon":-0.58932440},"state":"England","street":"Adrians Walk","zip":"SL2 5AT"},{"addressType":"sight","city":"~Slough","country":"United Kingdom","featureType":"Building a terrain of interest","name":"Slough","position":{"lat":51.51206867,"lon":-0.59121503},"state":"England","street":"Railway Terrace"},{"addressType":"sight","city":"~Slough","country":"United Kingdom","featureType":"Station","name":"Slough","position":{"lat":51.51225777,"lon":-0.59193386},"state":"England","street":"Railway Terrace"}],"status":{"duration":636,"procedure":"geo.location.search.2","success":true}}

I want to de-serialize this response into object. I am currently using following object.
but it is throwing error "Cannot deserialize JSON object into type 'OpenstreetMap.places[]'."

public class places
{
public string addressType { get; set; }
public string city { get; set; }
public string country { get; set; }
public string featureType { get; set; }
public List<object> position { get; set; }
public string state { get; set; }
public string street { get; set; }
public string zip { get; set; }
public List<object> status { get; set; }
}

And I am using below code for this:

string s =@"http://beta.geocoding.cloudmade.com/v3/0552b29114ef41a4ac10d06bf97939b4/api/geo.location.search.2?format=json&source=OSM&enc=UTF-8&limit=10&locale=en&q=24%20adrians%20walk,%20slough";
           var client = new RestClient(s);
           var request = new RestRequest(s);
           RestResponse response = client.Execute(request) as RestResponse;
           var json = response.Content; // raw content as string
           places[] obj = JsonConvert.DeserializeObject<places[]>(json);







谢谢



Umesh Tayade




Thanks

Umesh Tayade

推荐答案

尝试反序列化为以下内容:

Try deserializing into the following:
public class alist
{
   public Place[] places;
}
...
var obj = JsonConvert.DeserializeObject<alist>(json);
...
</alist>


大家好,



我已经解决了通过修改我的课程来解决问题如下:



Hi All,

I have solved the aboce issue by modifying my class as given below:

public class places
    {
        [JsonProperty("addressType")]
        public string AddressType { get; set; }
        [JsonProperty("city")]
        public string City { get; set; }
        [JsonProperty("country")]
        public string Country { get; set; }
        [JsonProperty("featureType")]
        public string FeatureType { get; set; }
        [JsonProperty("position")]
        public Position Positions { get; set; }
        [JsonProperty("state")]
        public string State { get; set; }
        [JsonProperty("street")]
        public string Street { get; set; }
        [JsonProperty("zip")]
        public string Zip { get; set; }
    }

    public class Position
    {
        public string lat { get; set; }
        public string lon { get; set; }
    }

    public class status
    {
        public int duration { get; set; }
        public string procedure { get; set; }
        public bool success { get; set; }
    }

    public class obJson
    {
        public places[] places { get; set; }
        [JsonProperty("status")]
        public status Status { get; set; }
    }









obJson obj = JsonConvert .DeserializeObject< objson>(json);



谢谢



Umesh Tayade




and
obJson obj = JsonConvert.DeserializeObject<objson>(json);

Thanks

Umesh Tayade


这篇关于将Json Response反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆