C# JSon 데이터를 솔리드 클래스로, 솔리드 클래스를 Json으로 변환

9154 단어 c#json
자세히 보기
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Script.Serialization;

namespace AjaxTest
{
    public partial class Json : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(getJsonInfo());
            //string strReg = "\\\\";
            //strReg = strReg.Replace("\\\\", "\\");
            ////Response.Write(strReg);
            //   
            Response.Write(disJsonInfo(getObjectByJson(getJsonInfo())));      //   
            Response.Write(JsonInfo.getInfo());

        }

        /// 
        ///          json  (               )
        /// 
        /// 
        public string getJsonInfo()
        {
            UserInfo userInfo = new UserInfo();
            //userInfo.strNameInfo = "  ";
            //userInfo.intAgeInfo = 23;
            //userInfo.intTelInfo = 66666;
            //userInfo.strAddrInfo = "   ";
            //userInfo.strPasswordInfo = "yhx.123";
            userInfo.strName = "  ";
            userInfo.intAge = 23;
            userInfo.strPsd = "yhx.123";
            userInfo.intTel = 2324;
            userInfo.strAddr = "   ";
            //      json
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInfo));
            //         
            System.IO.MemoryStream ms = new MemoryStream();
            // json         
            serializer.WriteObject(ms, userInfo);
            System.IO.StreamReader reader = new StreamReader(ms);
            ms.Position = 0;
            string strRes = reader.ReadToEnd();
            reader.Close();
            ms.Close();
            return strRes;
        }

        /// 
        ///  json           
        /// 
        /// 
        private static List getObjectByJson(string jsonString)
        {
            //    DataContractJsonSerializer  ,           
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List));
            // Json        
            jsonString = "[" + jsonString + "]";
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            //   ReadObject         
            object ob = serializer.ReadObject(stream);
            List ls = (List)ob;
            return ls;
        }

        /// 
        ///               
        /// 
        private string  disJsonInfo(List us)
        {
            string strItem = "";
            foreach(var item in us )
            {
                strItem += item.strName + ":" + item.strPsd + ":" + item.intAge + ":" + item.intTel + ":" + item.strAddr + "
"; } return strItem; } } /// /// /// public class UserInfo { // public string strName { get; set; } // public int intAge { get; set; } // public string strPsd { get; set; } // public int intTel { get; set; } // public string strAddr { get; set; } //// //public UserInfo() //{ // strName = ""; // intAge = 0; // strPsd = ""; // intTel = 0; // strAddr = ""; //} ///// ///// ///// //public string strNameInfo //{ // set { strName = value; } // get { return strName; } //} ///// ///// ///// //public int intAgeInfo //{ // set { intAge = value; } // get { return intAge; } //} ///// ///// ///// //public string strPasswordInfo //{ // set { strPsd = value; } // get { return strPsd; } //} ///// ///// ///// //public int intTelInfo //{ // set { intTel = value; } // get { return intTel; } //} ///// ///// ///// //public string strAddrInfo //{ // set { strAddr = value; } // get { return strAddr; } //} } /// /// json ( ) /// public static class JsonInfo { /// /// json ( ) /// /// public static string getJsonInfo() { UserInfo userInfo = new UserInfo(); userInfo.strName = " "; userInfo.intAge = 23; userInfo.strPsd = "yhx.123"; userInfo.intTel = 2324; userInfo.strAddr = " "; // json DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInfo)); // System.IO.MemoryStream ms = new MemoryStream(); // json serializer.WriteObject(ms, userInfo); System.IO.StreamReader reader = new StreamReader(ms); ms.Position = 0; string strRes = reader.ReadToEnd(); reader.Close(); ms.Close(); return strRes; } /// /// /// /// public static string getInfo() { string JsonStr = "["+getJsonInfo()+"]"; List products; products = JsonInfo.JSONStringToList(JsonStr); string strItem = ""; foreach (var item in products) { strItem += item.strName + ":" + item.strPsd + ":" + item.intAge + ":" + item.intTel + ":" + item.strAddr + "
"; } return strItem; } /// /// List /// /// /// /// public static List JSONStringToList(this string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List objs = Serializer.Deserialize>(JsonStr); return objs; } /// /// /// /// /// /// public static T Deserialize(string json) { T obj = Activator.CreateInstance(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } } } }

좋은 웹페이지 즐겨찾기