最新C# Object与json互转

在网上搜索C# Object与json互转,主要有两种方案,但是发现都不可用。


首先奉上调试正常可用的,net framework4.0,加上相应的dll库:Newtonsoft.Json.dll

using Core.Bases;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;


namespace Core.Text
{
    public class JsonTools
    {

        // 从一个对象信息生成Json串
        public static string ObjectToJson(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }
        // 从一个Json串生成对象信息
        public static object JsonToObject(string jsonString, object obj)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString, obj.GetType());
        }
    }
}


网上主要方案一:

public static APIResponse GetResponse(string str)
        {

            StringReader sr = new StringReader(str);
            JsonSerializer serializer = new JsonSerializer();
            return (APIResponse)serializer.Deserialize(new JsonTextReader(sr), typeof(APIResponse));
        }

        /// 
        /// 生成Json格式
        /// 
        /// 
        /// 
        /// 
        public static string GetJson(object obj)
        {
            if (obj == null)
                return "";

            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, obj);
                string szJson = Encoding.UTF8.GetString(stream.ToArray());
                return szJson;
            }
        }
        /// 
        /// 获取Json的Model
        /// 
        /// 
        /// 
        /// 
        public static T ParseFromJson(string szJson)
        {
            if (typeof(T) == typeof(IEnumerable<>))
            {

            }
            T obj = Activator.CreateInstance();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }

        /// 
        /// 获取Json的Model
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable ParseListFromJson(string szJson)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Deserialize>(szJson);

            //T obj = Activator.CreateInstance();
            //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            //{
            //    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            //    return (T)serializer.ReadObject(ms);
            //}
        }

这种方案运行出来字符串如下:

"{\"k__BackingField\":0,\"k__BackingField\":0,\"k__BackingField\":0,\"k__BackingField\":4,\"k__BackingField\":15.6384974,\"k__BackingField\":0,\"k__BackingField\":0,\"k__BackingField\":11.9906368,\"k__BackingField\":29.7924538,\"k__BackingField\":0,\"k__BackingField\":874,\"k__BackingField\":8092,\"k__BackingField\":0.268140942,\"k__BackingField\":0.471894622}"

不符合要求


方案二:

 // 从一个对象信息生成Json串
        public static string ObjectToJson(object obj)
        {
           return JavaScriptConvert.SerializeObject(obj);
        }
        // 从一个Json串生成对象信息
        public static object JsonToObject(string jsonString, object obj)
        {
           return JavaScriptConvert.DeserializeObject(jsonString, obj.GetType());
        }

虽然已经引入 Newtonsoft.Json.dll,但是由于net4.0中该方法已经过期,所以还是会报找不到的错误。解决方案,在最上面。


你可能感兴趣的:(Net开发)