【C#基础】json数据解析

时间:2025-01-21 16:06:09

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 =  };
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);