xmantium
xmantium

Reputation: 11

New to JSON.net, sample code

Im starting a new WP7 app that gets information from a DNLA server. The server supports JSON languages for the REST protocol. I did some research and found json.net was very well recommended.

I can get data by using http://192.168.1.1:234/rest/status?media=json Giving me: {"serverStatus":"STARTED","renderers":[{"uuid":"00a0965fa15c","ipAddress":"192.168.1.10","name":"KDL-52NX803","profileId":"9","status":"ACTIVE"},{"uuid":"1829220b083f","ipAddress":"192.168.1.13","name":"Windows Media Player","profileId":"1","status":"INACTIVE"},{"uuid":"5d70ac53cf8e","ipAddress":"192.168.1.14","name":"Unrecognized device","profileId":"1","status":"UNKNOWN"},{"uuid":"60465a95eec4","ipAddress":"192.168.1.22","name":"Playstation 3","profileId":"4","status":"UNKNOWN"},{"uuid":"001dd860bce4","ipAddress":"192.168.1.9","name":"Xbox 360","profileId":"3","status":"INACTIVE"}]}

Im very much new to c#, ive read the official Json.NET documenation, but would prefer to see sample code to get me moving. I have created listbox to collect "renderers" data, and TextBlock for the current "serverStatus" of the server.

If anyone can help and would very much appreciate your efforts

Upvotes: 1

Views: 5022

Answers (1)

Derek Beattie
Derek Beattie

Reputation: 9478

For starters, http://json2csharp.com/ is handy for putting in your json and creating a poco.

public class Renderer
{
    public string uuid { get; set; }
    public string ipAddress { get; set; }
    public string name { get; set; }
    public string profileId { get; set; }
    public string status { get; set; }
}

public class RootObject
{
    public string serverStatus { get; set; }
    public Renderer[] renderers { get; set; }
}

This page has simple examples on serialize/dserialize.

RestSharp is a wonderful HTTP API Client that supports WP7. It will deserialzie for you and also allows you to implement your own serializer/deserializer.

Upvotes: 5

Related Questions