SingletonBaseTemplate

public static byte[] writeValueAsZipByte(List<CraneDataDtls> dtls) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
            GZIPOutputStream outputStream = null;
            try {
                  outputStream = new GZIPOutputStream(byteOutputStream);
                  mapper.writeValue(outputStream, dtls);
            } catch (JsonGenerationException e) {
                  LOG.error("Error when convert Pojo to Json ", e);
                  throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
            } catch (JsonMappingException e) {
                  LOG.error("Error when convert Pojo to Json ", e);
                  throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
            } catch (IOException e) {
                  LOG.error("Error when convert Pojo to Json ", e);
                  throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
            }
            byte[] arr = byteOutputStream.toByteArray(); 
            return arr;
      }

Java 上述代码:

 

JSON 数据转换 :

代码:

//{
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
//}
static void Main(string[] args)
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" ,"Large"};
string json = JsonConvert.SerializeObject(product);
Debug.WriteLine(json);
Console.ReadKey();

Product productClone = JsonConvert.DeserializeObject<Product>(json);
Debug.WriteLine(productClone.Name);
Debug.WriteLine(productClone.Expiry);
for (int i = 0; i < productClone.Sizes.Length; i++)
{
Debug.Write(productClone.Sizes[i] + " ");
}
Console.ReadKey();

}

https://json.codeplex.com/downloads/get/832296

 

 http://www.cnblogs.com/piaopiao7891/p/3624779.html

 

    public class Customer
    {
        public string CustomerName;
        public int Unid;
    }
        public void CallCustomerToJson()
        {
            Customer cc = new Customer { Unid = 1, CustomerName = "John" };
            string strJson = CustomerToJsonSerialize(cc);

            Console.WriteLine(strJson);
        }

        public void CallJsonToCustomer()
        {
            string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
            Customer c = JsonToCustomerDeserialize(strJson) as Customer;

            Console.WriteLine(c.Unid + " " + c.CustomerName);
        }

        public void GenericCallCustomerToJson()
        {
            Customer cc = new Customer { Unid = 1, CustomerName = "John" };
            string strJsons = GenericToJson<Customer>(cc);
            Console.WriteLine(strJsons);
        }

        public void GenericCallJsonToCustomer()
        {
            string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
            Customer c = GenericFromJson<Customer>(strJson);
            Console.WriteLine(c.Unid + " " + c.CustomerName);
        }

        #region Json

        /// <summary>
        /// Json Serialize
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public string CustomerToJsonSerialize(Customer customer)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer));

            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, customer);

            string strReturn = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();

            return strReturn;
        }

        /// <summary>
        /// Json DeSerialize
        /// </summary>
        /// <param name="strJson"></param>
        /// <returns></returns>
        public object JsonToCustomerDeserialize(string strJson)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer));
            MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(strJson));

            return ds.ReadObject(ms);
        }


        /// <summary>
        /// GenericToJson
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GenericToJson<T>(T t)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, t);

            string strReturn = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return strReturn;
        }

        /// <summary>
        /// GenericFromJson
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strJson"></param>
        /// <returns></returns>
        public static T GenericFromJson<T>(string strJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));

            return ds.ReadObject(ms) as T;
        }

        #endregion

 

 

 

 

    [DataContract]//必须申明,否则无法序列化
    public class Person
    {
        [DataMember(Name = "PersonName")]//必须申明,否则无法序列化
        private string Name;

        [DataMember(Name = "PersonSex")]//必须申明,否则无法序列化
        private bool Sex;

        public Person(string name, bool sex)
        {
            this.Name = name;
            this.Sex = sex;
        }

        public override string ToString()
        {
            return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "" : "");
        }

    }

    [DataContract]
    public class Programmer : Person
    {
        [DataMember(Name = "ProgramLanguage")]
        private List<string> Languages;

        public Programmer(string name, bool sex, List<string> languages)
            : base(name,sex)
        {
            this.Languages = languages;
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            str.Append(base.ToString() + "\t编程语言:");
            foreach (string lang in this.Languages)
            {
                str.Append(lang + " ");
            }

            return str.ToString();
        }

    }


        public void DataContractSampleCode()
        {
            string filePath = @"D:\ObjectJson.txt";

            List<string> languages = null;
            languages = new List<string>(new string[] { "C#", "Java" });
            languages = new List<string>(new string[] { "C#", "C++" });
            languages = new List<string>(new string[] { "C#", "C++", "C", "Java" });

            List<Programmer> list = new List<Programmer>();
            list.Add(new Programmer("Coder1", true, languages));
            list.Add(new Programmer("Coder2", false, languages));
            list.Add(new Programmer("Coder3", true, languages));

            //序列化对象
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Programmer>));

            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, list);//将对象转换成json存储
            string jsonStr = Encoding.Default.GetString(ms.ToArray());
            File.WriteAllText(filePath, jsonStr);
            ms.Position = 0;
            ms.Dispose();//关闭内存流

            //反序列化对象
            list.Clear();

            using (FileStream fStream = File.Open(filePath,FileMode.OpenOrCreate))
            {
                list = (List<Programmer>)ser.ReadObject(fStream);
                foreach (Programmer p in list)
                {
                    Console.WriteLine(p);
                }
            }

        }

 

 

http://www.cnblogs.com/junbird-nest/archive/2012/03/23/2413441.html

http://www.cnblogs.com/phoenixtrees/archive/2011/05/01/2033847.html

 

 

posted on 2014-02-21 10:51  JackSlaterYu  阅读(240)  评论(0编辑  收藏  举报