c# 대상과 json 상호 전환

1828 단어 jsonC# 객체
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Script.Serialization;

namespace JsonDemo
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public void DoWork()
        {
        }
        /// <summary>
        /// 1)   json        (user)
        /// 2)             
        /// 3)         json     
        /// </summary>
        /// <param name="_userJson">  json   </param>
        /// <returns>  json   </returns>
        public string GetUser(string _userJson)
        {
            string resultJson = "";
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                User user = serializer.Deserialize<User>(_userJson);
                using (DataUserDataContext du = new DataUserDataContext())
                {
                    if (user.id != 0)
                    {
                        User item = du.Users.SingleOrDefault(u => u.id.Equals(user.id));
                        if (item != null)
                        {
                            resultJson = serializer.Serialize(item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return resultJson;
        }
    }
}

좋은 웹페이지 즐겨찾기