CSharp examples for System.Runtime.Serialization.Json:Json
Convert Object To Json
using System.Threading.Tasks; using System.Text; using System.Linq; using System.Collections.Generic; using System;/*from w w w. ja v a 2s . c o m*/ public class Main{ public static string ToJson<T>(List<T> lstData) { System.Text.StringBuilder strBuilder = new System.Text.StringBuilder(); System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); foreach (T city in lstData) { using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { serializer.WriteObject(memoryStream, city); strBuilder.Append(System.Text.Encoding.UTF8.GetString(memoryStream.ToArray())); } } return strBuilder.ToString(); } public static string ToJson<T>(object obj) { string result = String.Empty; System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { serializer.WriteObject(memoryStream, obj); result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); } return result; } }