Object to Bytes by Json - CSharp Newtonsoft.Json

CSharp examples for Newtonsoft.Json:JsonConvert.DeserializeObject

Description

Object to Bytes by Json

Demo Code


using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System;//  ww  w .j  a  va2s .c  o m
using Newtonsoft.Json;

public class Main{

        public static byte[] Object2Bytes(object obj)
        {
            string json = JsonConvert.SerializeObject(obj);
            byte[] serializedResult = System.Text.Encoding.UTF8.GetBytes(json);
            return serializedResult;
        }
}

Related Tutorials