c# 객체를 json 형식으로 변환

2720 단어 c#
객체 형식
public class UpdatePnrVO
    {
        public long no { get; set; }
        public string status { get; set; }
        public List apvInfoVOList { get; set; }
    }

public class ApvInfoVOList
    {
        public long applyno { get; set; }      
        public List apverInfos { get; set; }
    }
    public class ApverInfos
    {
        public string apvername { get; set; }
        public string apvmobile { get; set; }
    }

객체 초기화
            UpdatePnrVO upvo=new UpdatePnrVO();
            #region  
            upvo.no = 2135468;
            upvo.status = "11";         
            var apverInfos = new ApverInfos();
            apverInfos.apvername = "jkli";
            apverInfos.apvmobile = "22222222222";
            var apvInfoVOList = new ApvInfoVOList();
            apvInfoVOList.applyno = 111222;
            apvInfoVOList.apverInfos.Add(apverInfos);
            upvo.apvInfoVOList=new List();            
            upvo.apvInfoVOList.Add(apvInfoVOList);
            #endregion
            var jsonText = UpdatePnrAPI.SynchronousPnr(upvo);

호출 방법
4
public static bool SynchronousPnr(UpdatePnrVO upvo)
        {
            var json = new StringBuilder("{");
            json.AppendFormat("\"no\": {0},", upvo.no);
            json.AppendFormat("\"status\": {0},", upvo.status);
            json.Append("\"apvInfoVOList\": [");
            foreach (var t0 in upvo.apvInfoVOList)
            {
                json.AppendFormat("{{\"applyno\": \"{0}\",", t0.applyno);
                #region
                json.Append("\"apverInfos\": [");
                foreach (var t1 in t0.apverInfos)
                {
                    json.AppendFormat("{{\"apvername\": \"{0}\",", t1.apvername);
                    json.AppendFormat("\"apvmobile\": \"{0}\",", t1.apvmobile);
                }
                json = json.Remove(json.Length - 1, 1);
                json.Append("}],");
                #endregion
            }
            json = json.Remove(json.Length - 1, 1);
            json.Append("}]}");
            var reqJson = json + "";
            var result = HttpRequest.Request("http://XXXXXXX", reqJson);
            var boolResult = result.IndexOf("\"result\":\"FAIL\"", StringComparison.Ordinal) < 0;            
            return boolResult;
        }
마지막 몇 걸음은 되돌아오는 결과를 해석하는 데 쓰인다

좋은 웹페이지 즐겨찾기