【C#基础】json数据解析

1、简单的获取某个键值

JToken jtoken = JToken.Parse(jsonStr);
string jsjid = jtoken.Value<string>("JSJID");

多层次对象解析

            string json = KTHttpRequest._Post(AirwayKeeperModel.YiChengNewMemberUrl, "_OperateMemberOraInfo", new
            {
                CommandStr = "delete_user_info",
                Mobile = nMobile
            });
            var jtoken = JToken.Parse(json);
            if (null != jtoken)
            {
                var baseRes = jtoken.Value<dynamic>("BaseResponse");
                if (null != baseRes)
                {
                    if (baseRes.Value<bool>("IsSuccess"))
                        return true;

                    throw new JSJException(baseRes.Value<string>("ErrorMessage"));
                }
            }

            return false;

 

2、序列化

object nPostData = new { BaseRequest = new BaseRequest(), JSJID = 9527 };
string json = Newtonsoft.Json.JsonConvert.SerializeObject(nPostData);

3、反序列化

            string tmp = KTHttpRequest._Post(m_ConfigModel.WShareUrl, "_GetCodeStrByOrderNum", new
            {
                OrderNum = orderNum,
            });
            var response = Newtonsoft.Json.JsonConvert.DeserializeObject<GetCodeStrByOrderNumResponse>(tmp);

 

posted @ 2016-10-09 15:12  0539  阅读(1015)  评论(0编辑  收藏  举报