无法将JSON响应映射到C#/动态属性名称中的预定义类

时间:2017-03-30 16:25:17

标签: c# asp.net json asp.net-mvc asp.net-mvc-5

我有一个基本上包含这样的属性的类:

public class Msg
{
    [JsonProperty(PropertyName = "XD1703301059485299")]
    public Shipping shipping { get; set; }
}

问题出在这一部分:

 [JsonProperty(PropertyName = "XD1703301059485299")]

我从服务器获取的动态属性名称......

此属性名称可以是服务器返回的任何名称。在这种特殊情况下,它能够将JSON映射到我的类,因为属性名称相同......但是当服务器返回类似这样的内容时:

XS12394124912841

该对象为null ....

如何将属性名称解析为动态?有人可以帮助我吗?

P.S。这是JSON响应本身:

{"status":1,"msg":{"dynamic_name":{"order_sn":"12312313123123123","order_status":"0","shipping_info":[{"shipping_name":"","shipping_no":"","shipping_img":"","shipping_code":"","shipping_time":"","track_goods":""}]}},"errcode":0}

2 个答案:

答案 0 :(得分:1)

您可以在System.Web.Helpers

的帮助下实现类似的功能
<div class="row">
   </br>
 </div>

这里sample.json包含您的示例JSON响应。

答案 1 :(得分:1)

所以我认为这个问题不像听起来那么动态。您可以只转换为dyanmic对象并显式处理转换。

以下示例解决方案。我插入了一些值来显示转换按预期工作。

添加nuget包Newtonsoft.Json

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace Serialize
{
    public class Shipping
    {
        [JsonProperty(PropertyName = "shipping_name")]
        public String Name { get; set; }

        [JsonProperty(PropertyName = "shipping_img")]
        public String Img { get; set; }

        [JsonProperty(PropertyName = "shipping_code")]
        public String Code { get; set; }
    }

    public class Order
    {
        public Shipping shipping { get; set; }

        [JsonProperty(PropertyName = "order_sn")]
        public string SerialNumber { get; set; }

        [JsonProperty(PropertyName = "order_status")]
        public string Status { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /*
             {
             "status":1,
             "msg": {
                 "dynamic_name": {
                     "order_sn": "12312313123123123",
                     "order_status":"0",
                     "shipping_info": [{
                         "shipping_name":"name",
                         "shipping_no":"",
                         "shipping_img":"img",
                         "shipping_code":"code",
                         "shipping_time":"",
                         "track_goods":""
                     }]
                 }
             },
             "errcode":0
             }
             * */
            var raw = "{ \"status\":1, \"msg\":{\"dynamic_name\":{\"order_sn\":\"12312313123123123\",\"order_status\":\"0\",\"shipping_info\":[{\"shipping_name\":\"name\",\"shipping_no\":\"\",\"shipping_img\":\"img\",\"shipping_code\":\"code\",\"shipping_time\":\"\",\"track_goods\":\"\"}]}},\"errcode\":0}";

            var incomingOrder = new Order();

            // properties on dynamic objects are evaluated at runtime
            dynamic msgJson = JObject.Parse(raw);

            // you'll want exception handling around all of this
            var order = msgJson.msg.dynamic_name;

            // accessing properties is easy (if they exist, as these do)
            incomingOrder.SerialNumber = order.order_sn;
            incomingOrder.Status = order.order_status;

            // JObject cast might not be necessary. need to check for array elements, etc.
            // but it's simple to serialize into a known type
            incomingOrder.shipping = ((JObject)(order.shipping_info[0])).ToObject<Shipping>();

        }
    }
}

或者,如果在运行时给出属性名称,则可以使用索引器getter取消引用属性

        dynamic msgJson = JObject.Parse(raw);

        JObject order = msgJson.msg["XS12394124912841"];

        incomingOrder.SerialNumber = order["order_sn"].ToObject<string>();
        incomingOrder.Status = order["order_status"].ToObject<string>();
        incomingOrder.shipping = order["shipping_info"][0].ToObject<Shipping>();