Deserialize array values to .NET properties using DataContractJsonSerializer

Posted by James Cadd on Stack Overflow See other posts from Stack Overflow or by James Cadd
Published on 2010-04-26T20:42:56Z Indexed on 2010/05/01 13:47 UTC
Read the original article Hit count: 226

Filed under:
|
|
|

I'm working with the DataContractJsonSerializer in Silverlight 4 and would like to deserialize the following JSON:

{
    "collectionname":"Books",
    "collectionitems": [
            ["12345-67890",201,
             "Book One"],
            ["09876-54321",45,
             "Book Two"]
        ]
}

Into classes like the following:

class BookCollection
{
  public string collectionname { get; set; }
  public List<Book> collectionitems { get; set; }
}

class Book
{
  public string Id { get; set; }
  public int NumberOfPages { get; set; }
  public string Title { get; set; }
}

What's the proper place to extend DataContractJsonSerializer to map the unnamed first array element in "collectionitems" to the Id property of the Book class, the second element to the NumberOfPages property and the final element to Title? I don't have control over the JSON generation in this instance and would like the solution to work with the Silverlight subset of .NET. It would be great if the solution could perform the reverse for serialization as well.

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON