0

This is kind of a two part question. I suppose the first is, am I going the best way about doing this and the second is the roadblock I've hit. The following code works for string properties but not for example an int. I get the int value out of the KVP as a string but I can't set an int to a string on the property. I can't find the syntax that lets me parse.

[WebInvoke(UriTemplate = "", Method = "POST")]
public Response Post(JsonValue items)
{
    List<Provider> providers = new List<Provider>();
    foreach (var item in items)
    {
        var json = item.Value;
        var provider = new Provider();

        foreach (var property in typeof(Provider).GetProperties())
        {
            if (json.ContainsKey(property.Name))
                property.SetValue(provider, json[property.Name].ToString(), null);
        }

        providers.Add(provider);
    }

    return new Response { success = true, data = providers };
}
Daniel Revell
  • 8,338
  • 14
  • 57
  • 95

1 Answers1

1

You could do a switch over property.PropertyType and parse the String accordingly.

However, I would not recommend to write your own JSON deserializer. Use the DataContract API: Tutorial.

marc
  • 6,103
  • 1
  • 28
  • 33