Json.NET使用入门(五)【复杂Json反序列化】

人一旦觉悟,就会放弃追寻身外之物,而开始追寻内心世界的真正财富。


相关实体类:

public  class RsponseJson
    {

        [JsonProperty("msgType")]
        public string MsgType { get; set; }

        [JsonProperty("msgObj")]
        public string MsgObj { get; set; }
    }

 public class MsgObjModel
    {

        [JsonProperty("sign")]
        public string Sign { get; set; }

        [JsonProperty("params")]
        public DicPara dicPara { get; set; }
    }

 public  class DicPara
    {

        [JsonProperty("plateNo")]
        public string PlateNo { get; set; }

        [JsonProperty("parkIndexcode")]
        public string ParkIndexcode { get; set; }

        [JsonProperty("preFee")]
        public decimal PreFee { get; set; }

        [JsonProperty("action")]
        public string Action { get; set; }

        [JsonProperty("cloudEnterId")]
        public string CloudEnterId { get; set; }

        [JsonProperty("orderCode")]
        public string OrderCode { get; set; }

        [JsonProperty("cloudParkId")]
        public string CloudParkId { get; set; }
    }

Program.CS代码:

 class Program
    {
        static void Main(string[] args)
        {
            //方法一
            string jsonstr = @"{""msgType"":""ORDER_PAY_SUCCESS"",""msgObj"":""{\""sign\"":\""1e7068bacxxxxxxxxxxxxxxxxxxxxxxx\"",\""params\"":{\""plateNo\"":\""浙A99999\"",\""parkIndexcode\"":\""111\"",\""preFee\"":100.00,\""action\"":\""queryPreFee\"",\""cloudEnterId\"":\""111\"",\""orderCode\"":\""0719141034-6481\"",\""cloudParkId\"":\""PI1482457208756202110\""}}""}";


            RsponseJson Jm = JsonConvert.DeserializeObject<RsponseJson>(jsonstr);
            MsgObjModel jmodel = JsonConvert.DeserializeObject<MsgObjModel>(Jm.MsgObj);
        }
    }

 static void Main(string[] args)
        {
           //方法二,无需任何实体类
            string jsonstr = @"{""msgType"":""ORDER_PAY_SUCCESS"",""msgObj"":""{\""sign\"":\""1e7068bacxxxxxxxxxxxxxxxxxxxxxxx\"",\""params\"":{\""plateNo\"":\""浙A99999\"",\""parkIndexcode\"":\""111\"",\""preFee\"":100.00,\""action\"":\""queryPreFee\"",\""cloudEnterId\"":\""111\"",\""orderCode\"":\""0719141034-6481\"",\""cloudParkId\"":\""PI1482457208756202110\""}}""}";

            JObject jsonSearch = JObject.Parse(jsonstr);
            string haha = jsonSearch["msgType"].ToString();
            string  msgObjstr = jsonSearch["msgObj"].ToString();

            JObject msgObjModel= JObject.Parse(msgObjstr);
            string chepai = msgObjModel["sign"].ToString();
            string plateNo = msgObjModel["params"]["plateNo"].ToString();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值