selladurai
selladurai

Reputation: 6789

How to create class for JSON web service result in windows phone 7?

how can I create class for JSON web service result? This is my JSON result which i get from google api. so, how to create class and deserialization for this JSON object. alt text

I created class to deserialization this JSON object like this,

string mapdetail = e.Result;
        var djosn = new DataContractJsonSerializer(typeof(mapResult));
        var mstr = new MemoryStream(Encoding.UTF8.GetBytes(mapdetail));
        mapResult reslt = (mapResult)djosn.ReadObject(mstr);

mapResult class:

[DataContract]
public class mapResult
{
    [DataMember]
    public string status
    {
        get;
        set;
    }
    [DataMember]
    public IList<Resultdetail> result
    {
        get;
        set;
    }
}

So i creadet list for result details and others:

[DataContract]
public class Resultdetail
{
    [DataMember]
    public List<string> types
    {
        get;
        set;
    }
    [DataMember]
    public string formatted_address
    {
        get;
        set;
    }
    [DataMember]
    public List<object> address_components
    {
        get;
        set;
    }
    [DataMember]
    public List<Geometry> geometry
    {
        get;
        set;
    }

}

[DataContract]
public class Geometry
{
    [DataMember]
    public List<GeoLocation> location
    {
        get;
        set;
    }
    [DataMember]
    public string location_type
    {
        get;
        set;
    }
    [DataMember]
    public List<object> viewport
    {
        get;
        set;
    }
    [DataMember]
    public List<object> bounds
    {
        get;
        set;
    }

}

[DataContract]
public class GeoLocation
{
    [DataMember]
    public double lat
    {
        get;
        set;
    }
    [DataMember]
    public double lng
    {
        get;
        set;
    }

}

Now I'm getting Null reference;

alt text

Upvotes: 1

Views: 1829

Answers (2)

Matt Lacey
Matt Lacey

Reputation: 65586

There were a few issues with your DatContract classes.

I've corrected these below, commented out the original lines and added a comment about what was specifically wrong.

[DataContract]
public class mapResult
{
    [DataMember]
    public string status { get; set; }
    [DataMember]
    //public IList<Resultdetail> result { get; set; }
    // Misspelt property name and use of interface rather than concrete type
    public List<Resultdetail> results { get; set; }
}

[DataContract]
public class Resultdetail
{
    [DataMember]
    public List<string> types { get; set; }
    [DataMember]
    public string formatted_address { get; set; }
    [DataMember]
    public List<object> address_components { get; set; }
    [DataMember]
    //public List<Geometry> geometry { get; set; }
    // Json does not contain an array/list of these
    public Geometry geometry { get; set; }
}

[DataContract]
public class Geometry
{
    [DataMember]
    //public List<GeoLocation> location { get; set; }
    // Json does not contain an array/list of these
    public GeoLocation location { get; set; }
    [DataMember]
    public string location_type { get; set; }
    [DataMember]
    // public List<object> viewport { get; set; }  
    // Json does not contain an array/list of these
    public object viewport { get; set; }
    [DataMember]
    //public List<object> bounds { get; set; }
    // Json does not contain an array/list of these
    public object bounds { get; set; }
}

The following code shows that this works:

var jsonStr = "{\"status\": \"OK\", \"results\": [ { \"types\": [ \"route\" ], \"formatted_address\": \"7th Main Rd, Koramangala, sengaluru, Karnataka 560034, India\", \"address_components\": [ { \"long_name\": \"7th Main Rd\", \"short_name\": \"7th Main Rd\", \"types\": [ \"route\" ] }, { \"long_name\": \"Koramangala\", \"short_name\": \"Koramangala\", \"types\": [ \"sublocality\", \"political\" ] }, { \"long_name\": \"Bengaluru\", \"short_name\": \"Bengaluru\", \"types\": [ \"locality\", \"political\" ] }, { \"long_name\": \"sengaluru\", \"short_name\": \"sengaluru\", \"types\": [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\": \"Karnataka\", \"short_name\": \"Karnataka\", \"types\": [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\": \"India\", \"short_name\": \"IN\", \"types\": [ \"country\", \"political\" ] }, { \"long_name\": \"560034\", \"short_name\": \"560034\", \"types\": [ \"postal_code\" ] }],\"geometry\": { \"location\":{ \"lat\": 12.9259085, \"lng\": 77.6334715 }, \"location_type\": \"GEOMETRIC_CENTER\", \"viewport\": { \"southwest\": { \"lat\": 12.9227118, \"lng\": 77.6301852 }, \"northeast\": { \"lat\": 12.929007, \"lng\": 77.6364805}}, \"bounds\": { \"southwest\": { \"lat\": 12.9247615, \"lng\": 77.6330486 },\"northeast\": { \"lat\": 12.9269574, \"lng\": 77.6336171 }}}}]}";

// If using json.net (http://json.codeplex.com/)
var des = JsonConvert.DeserializeObject<mapResult>(jsonStr);

// If using System.Runtime.Serialization.Json
var djosn = new DataContractJsonSerializer(typeof(mapResult));
var mstr = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
des = (mapResult)djosn.ReadObject(mstr);

var latitude = des.results[0].geometry.location.lat;
var longitude = des.results[0].geometry.location.lng;

Note that my code is tested with your sample json object only and is not guaranteed to work with everything returned by the web service. You're also deserialising quite a lot to object which may be awkward to work with if you want more than just the latitude and longitude.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1502935

I note that your top-level list is called result whereas the JSON name is results btw - could it be as simple as that? Maybe the deserializer is ignoring the results value because you don't have a property of that name.

If that doesn't work... You've got 7 dereferencing operations in that statement. (4 obvious ones and 3 array indexing operations). That's an awful lot of possible failure points. I suggest you put a break point on that statement (to break before it executes) and then use the watch window to look at what results you've actually got.

(Personally I've used Json.NET in Windows Phone 7 rather than the built-in libraries, but I'm sure the latter should work too.)

Upvotes: 2

Related Questions