From Json String to Object - CSharp System.Runtime.Serialization.Json

CSharp examples for System.Runtime.Serialization.Json:Json

Description

From Json String to Object

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*  w  w  w  .j  a  v a 2 s .  com*/

public class Main{

        public static T FormJson<T>(string jsonStr)
        {
            T obj = Activator.CreateInstance<T>();
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr)))
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(memoryStream);
            }
        }
}

Related Tutorials