zoukankan      html  css  js  c++  java
  • c#中jeson字符串和OBJECT对象的相互转换

    对于本问题   我用三步来分别说明实现过程

    1、定义一个类---- 实现转换的具体方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Json;
    
    namespace Test.JesonTest
    {
        public static class JsonHelper
        {
            //定义json操作运算
    
            //将字jeson字符串转换为object对象
            public static T strToObject<T>(string jsonString)
            {
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
                {
                    return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
                }
            }
    
            //将object对象反序列为jeson字符串
            public static string objectToStr(object jsonObject)
            {
                using (var ms = new MemoryStream())
                {
                    new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
        }
    }
    

      

      说明:首先,当然是项目是3.5+的;必须添加引用:System.Runtime.Serialization 和 System.ServiceModel

    2、创建一个OBJECT对象类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Test.JesonTest
    {
        public class Person
        {
            public int id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
    
           public Person() {
               id = 0;
               name = "xuhyuan";
               age = 88;
            }
        }
    }

      3、具体调用实现实例

          前台.aspx页面html。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Testjeson.aspx.cs" Inherits="Test.JesonTest.Testjeson" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <label style="color: Purple">
                将Jeson字符串和Object之间的相互转换实现</label>
            <br />
            <br />
            <br />
            <br />
            <asp:Button ID="but_str_obj" runat="server" Text="将jeson字符串转换为Object" 
                onclick="but_str_obj_Click" />
                  
            <asp:Button ID="but_obj_str" runat="server" Text="将Object换为jeson字符串转" 
                onclick="but_obj_str_Click" />
            <br /><br />
            <asp:textbox ID="txt_resoult" runat="server" Height="207px" 
                TextMode="MultiLine" Width="624px"></asp:textbox>
        </div>
        </form>
    </body>
    </html>
    

      c#页面后台代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Script.Serialization;
    
    namespace Test.JesonTest
    {
        public partial class Testjeson : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            //将jeson字符串转换为object
            protected void but_str_obj_Click(object sender, EventArgs e)
            {
                string str_jeson = txt_resoult.Text.Trim();
    
                if (!string.IsNullOrEmpty(str_jeson))
                {
                    List<Person> pes=new List<Person>();
                    pes = JsonHelper.strToObject<List<Person>>(str_jeson);
                   str_jeson="";
                    for (int i = 0; i < pes.Count; i++)
                    {
                        str_jeson += "id=" + pes[i].id + ";name=" + pes[i].name + ";age=" + pes[i].age + "<br /><br /><br />";
                    }
                }
    
                txt_resoult.Text+=str_jeson;
            }
    
            //将object转换为jeson字符串
            protected void but_obj_str_Click(object sender, EventArgs e)
            {
                Person pe = new Person();
                List<Person> pes = new List<Person>();
                pes.Add(pe);
                pes.Add(pe); pes.Add(pe);
               
                //反序列化实现
                string json = JsonHelper.objectToStr(pes);
                txt_resoult.Text = json;
    
                // 序列化----利用js实现
                //JavaScriptSerializer jss = new JavaScriptSerializer();
            }
        }
    }
  • 相关阅读:
    算法探究-2.retinaNet(Focal Loss)
    C++基础-枚举体 enum class
    C++基础-TypeTraits(进行类型的属性判断) 1.is_lvalue_reference(左值引用判断) 2.is_integral(整形判断) 3.is_class(基本类型判段) 4.is_same(判断类型一致) 5.enable_if(条件判断)
    C++基础-auto(自动分配属性)和decltype(指定分配属性)
    C++基础-正则实战(日期拆分regex_match ,符号拆分sregex_token_iterator, 邮箱的查找 regex_search)
    C++基础-正则表达式 regex_match(匹配) regex_search(查找) regex_replace(替换)
    Shell 入门(三):sed,awk,grep
    Shell 入门(二):数组与函数
    Shell 入门(一):变量和流程控制
    ArcSDE 版本差异提取
  • 原文地址:https://www.cnblogs.com/xiaoXuZhi/p/3223287.html
Copyright © 2011-2022 走看看