Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
XML
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);



Thanks

Umesh Tayade
Posted
Comments
Kornfeld Eliyahu Peter 1-Oct-13 4:58am    
The answer you got is an object with two properties
1. places - an array of objects
2. status - a single object
You try to deserialize it as status is property of places...
Umesh Tayade 1-Oct-13 5:41am    
Hi Kornfeld,

Thanks for response.

I tried above code but its not working.
I also tried by modifying my class as 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 List<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);

but it is throwing error as "Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[OpenstreetMap.Position]'."

Thanks

Umesh Tayade

Try deserializing into the following:
C#
public class alist
{
   public Place[] places;
}
...
var obj = JsonConvert.DeserializeObject<alist>(json);
...
</alist>
 
Share this answer
 
Comments
Umesh Tayade 1-Oct-13 5:41am    
Hi Mehdi,

Thanks for response.

I tried above code but its not working.
I also tried by modifying my class as 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 List<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);

but it is throwing error as "Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[OpenstreetMap.Position]'."

Thanks

Umesh Tayade
Hi All,

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

SQL
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; }
    }



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

Thanks

Umesh Tayade
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900