1

How do I parse/serialize following JSON in Silverlight?

{
    "total": 1050769,
    "page": 1,
    "pagesize": 30,
    "questions": [
    {
        "tags": [
        "camera",
        "detection",
        "droid"
        ],
        "answer_count": 0,
        "favorite_count": 0,
        "creation_date": 1288984683,
        "last_activity_date": 1288984683,
        "up_vote_count": 0,
        "down_vote_count": 0,
        "view_count": 0,
        "title": "Using face detect in your program"
    },
    {
        "tags": [
        "unit-testing",
        "spring",
        "spring-mvc"
        ],
        "answer_count": 1,
        "favorite_count": 0,
        "creation_date": 1288983038,
        "last_edit_date": 1288984681,
        "last_activity_date": 1288984681,
        "up_vote_count": 1,
        "down_vote_count": 0,
        "view_count": 5,
        "title": "Unit test for Spring MVC Controllers that use annotation @RequestParam"
    }]
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66

2 Answers2

2

There are a number of options, you state "parse/serialise" it would better if you picked one. Do you want to simple parse arbitary JSON? In that case use JsonValue from System.Json:-

 JsonValue root = JsonValue.Parse(jsonString);

If you have a set of .NET types matching the Json into which you want to deserialise then you need to use the DataContractJsonSeralizer in the System.Runtime.Serialization.Json namespace.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • serialization is better, and for this I had created the set of .Net types matching the json. but the problem is that I do not find `DataContractJsonSeralizer` in `System.Runtime.Serialization.Json` namespace in `Silverlight 4.0`. – Zain Shaikh Nov 05 '10 at 21:05
  • 1
    @Zain: you need to add a reference to the System.ServiceModel.Web.dll – AnthonyWJones Nov 06 '10 at 13:01
  • I have added reference to `System.ServiceModel.Web.dll` but I still do not find `DataContractJsonSeralizer` in it in Silverlight 4.0. :( – Zain Shaikh Nov 06 '10 at 13:58
  • @Zain: Have your tried it out the spelling error, i.e., `DataContractJsonSerializer` – AnthonyWJones Nov 06 '10 at 17:21
  • yes, actually I using the `intellisense` of VS2010, and when I write `System.ServiceModel.Web` so I do not find any data contract serializer for json. – Zain Shaikh Nov 06 '10 at 17:57
0

If you don't have a strongly typed object to pass it to, you can probably use DynamicObject, as long as the Silverlight mscorlib has access to that, which I think it would:

http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

[edit] This is just another tree to bark up...the other poster's link to JSON.NET is probably more the direction I would go in my own code...but I like strongly typed classes.

Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42