1

I am working with wp7 and even though when I type "dynamic" in VS highlights and lets me compile and run the application, but as soon as I try to use it I get compile errors.

I read I can't use dynamic keyword and now kinda lost on how to do my json parsing(I am using json.net and restsharp but both run into the same problem if I can't use dynamic)

For example say if I use foursquare api. All json data is always returned like

 {
          "meta": {
            "code": 200,
             ...errorType and errorDetail...
          },
          "notifications": {
             ...notifications...
          },
          "response": {
             ...results...
          }
        }

but the response will have different data. For instance it might have user data(User Class) or it might have venue data(Venue class).

The bottom line though is I am going to need a class called Response that is in a RootObject class.

But I can't have the same class name(unless I start putting them in different name spaces but not crazy about that idea).

Only other thing I can think of what sucks too is

public class RootObject
{
    public Response Response { get; set; }
}

public class Response
{
    public User User { get; set; }
    public List<Venue> Venues { get; set; }
}

This response object will in the end have all the different classes that could come back and in reality only property in the Response object will probably be filled.

Is there a better way?

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Windows Phone 7 doesn't support `dynamic`. See [Does Windows Phone 7 support the dynamic keyword?](http://stackoverflow.com/questions/4581224/does-windows-phone-7-support-the-dynamic-keyword). – Dustin Kingen Jul 07 '13 at 04:56

1 Answers1

2

Alright it took me forever to get a sample together (I hate OAuth), but you can use the JsonConverter with an interface to parse the responses.

See:

Here is an example for the User and Venues:

Converter:

public class ResponseConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(IResponse));
    }

    public override object ReadJson(JsonReader reader,
                                    Type objectType,
                                    object existingValue,
                                    JsonSerializer serializer)
    {
        // reader is forward only so we need to parse it
        var jObject = JObject.Load(reader);

        if(jObject["user"] != null)
        {
            var user = jObject.ToObject<UserResponse>();
            return user;
        }

        if(jObject["venues"] != null)
        {
            var venue = jObject.ToObject<VenuesResponse>();
            return venue;
        }

        throw new NotImplementedException("This reponse type is not implemented");
    }

    public override void WriteJson(JsonWriter writer,
                                   object value,
                                   JsonSerializer serializer)
    {
        // Left as an exercise to the reader :)
        throw new NotImplementedException();
    }
}

DTO:

public class ApiRespose
{
    public ApiRespose()
    {
        Notifications = new List<Notification>();
    }

    [JsonProperty("meta")]
    public Meta Meta { get; set; }

    [JsonProperty("notifications")]
    public List<Notification> Notifications { get; set; }

    [JsonProperty("response")]
    [JsonConverter(typeof(ResponseConverter))]
    public IResponse Response { get; set; }
}

public interface IResponse
{
}

public class UserResponse : IResponse
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("firstname")]
    public string FirstName { get; set; }

    [JsonProperty("lastname")]
    public string LastName { get; set; }
    // Other properties
}

public class VenuesResponse : IResponse
{
    public VenuesResponse()
    {
        Venues = new List<Venue>();
    }

    [JsonProperty("venues")]
    public List<Venue> Venues { get; set; }
}

public class Venue
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
    // Other Properties
}

public class Meta
{
    [JsonProperty("code")]
    public int Code { get; set; }
}

public class Notification
{
    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("item")]
    public Item Item { get; set; }
}

public class Item
{
    [JsonProperty("unreadcount")]
    public int UnreadCount { get; set; }
}

Usage:

string jsonData = GetJsonData();

var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(json);
Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92