How to parse this String using System.Runtime.Serialization.Json?

c#, json, parsing

Solution

Here's a quick sample I whipped up which appears to work:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

class Test
{
    static void Main()
    {
        using (Stream stream = File.OpenRead("test.json"))
        {
            var serializer = new DataContractJsonSerializer(typeof(Library));
            Library library = (Library) serializer.ReadObject(stream);
            Console.WriteLine(library.Books[0].Name);
        }

    }
}

[DataContract]
class Book
{
    [DataMember(Name="name")] public string Name { get; set; }
    [DataMember(Name="orig")] public string Orig { get; set; }
    [DataMember(Name="date")] public string Date { get; set; }
    [DataMember(Name="lang")] public string Lang { get; set; }
}

[DataContract]
class Library
{
    [DataMember(Name="books")] public IList<Book> Books { get; set; }
    [DataMember(Name="src")] public string Src { get; set; }
    [DataMember(Name="id")] public string Id { get; set; }
}

I'm sure there are plenty of other options you can tweak, but that should at least get you started.

Problem

Can you help me how to use `System.Runtime.Serialization.Json` (not `Json.NET`) to get information of each book in this tring to array: ``` { "books": [ {"name":"Book 1","orig":"Author 1","date":2009,"lang":"en"}, {"name":"Book 2","orig":"Author 2","date":2012,"lang":"fr"} ], "src":"lib", "id":212 } ```

Original source