4

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
}

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, if in other string have "score":9.080557e-06 but I cant parse it. What type I cant parse to?
@TuyenTk: Looks like you should be able to parse it to a double.
1

A generic approach:

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace YourApp.Helpers
{
    public class SystemSerializer:ISerializer
    {
        public T Deserialize<T>(string json) where T : class
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                return ser.ReadObject(ms) as T;
            }
        }

        public T Deserialize<T>(Stream json) where T : class
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            return ser.ReadObject(json) as T;
        }

        public async Task<string> SerializeAsync<T>(T obj) where T : class
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (var ms = new MemoryStream())
            {
                ser.WriteObject(ms, obj);
                ms.Position = 0;
                using (var sr = new StreamReader(ms))
                {
                    return await sr.ReadToEndAsync();
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.