Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

c#

parsing

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
}
like image 575
Thanh Nguyen Avatar asked Dec 15 '22 12:12

Thanh Nguyen


2 Answers

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.

like image 141
Jon Skeet Avatar answered Mar 27 '23 01:03

Jon Skeet


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();
                }
            }
        }
    }
}
like image 35
mattinsalto Avatar answered Mar 27 '23 00:03

mattinsalto