在另一个对象中序列化一组int

时间:2017-01-30 15:13:36

标签: c# arrays json

我们使用自定义序列化程序将c#对象序列化为json格式,并使用REST API进行intrerface。 API期望以下列格式接收数据:

    "product": {
        "sku": "211554",
        "extras": [{
                "code": "cost",
                "value": "3.99"
            }, {
                "code": "start_date",
                "value": "2017-01-31T14:27:49.3032432+00:00"
            }, {
                "code": "end_date",
                "value": "2017-02-01T14:27:49.3042537+00:00"
            }, {
                "code": "ids",
                "value": [
                    "1",
                    "2",
                    "3",
                    "19"
                ]
            }
        ]
    }

序列化程序的一部分只返回

的结果
JsonConvert.SerializeObject(obj, _Settings);

但结果

"product": {
            "sku": "211554",
            "extras": [{
                    "code": "cost",
                    "value": "3.99"
                }, {
                    "code": "start_date",
                    "value": "2017-01-31T14:56:05.4683698+00:00"
                }, {
                    "code": "end_date",
                    "value": "2017-02-01T14:56:05.4693696+00:00"
                }, {
                    "code": "ids",
                    "value": "System.Int32[]"
                }
            ]
        }

序列化程序似乎可以在复杂的嵌套对象类型上正常工作,但不能用于简单的嵌套类型,例如上面“ids”部分中的int数组。

知道需要做些什么才能让串行器递归地处理简单的嵌套类型?

更新

这是自定义序列化程序:

        public class CustomSerializer : ISerializer, IDeserializer
        {
            private JsonSerializerSettings _Settings;

            public CustomSerializer()
            {
                ContentType = "application/json";

                _Settings = new JsonSerializerSettings
                {
                    MissingMemberHandling = MissingMemberHandling.Ignore,
                    NullValueHandling = NullValueHandling.Ignore,
                    DefaultValueHandling = DefaultValueHandling.Include
                };
            }


            public string Serialize(object obj)
            {
                var result = JsonConvert.SerializeObject(obj, _Settings);
                return result;
            }

            public T Deserialize<T>(IRestResponse response)
            {
                if (response == null) return default(T);
                if (response.Content.StartsWith("{\"messages\":{")) return default(T);

                return response.Content == "[]" ? default(T) : JsonConvert.DeserializeObject<T>(response.Content, _Settings);
            }

...

    }

2 个答案:

答案 0 :(得分:1)

据我所知,它与您的自定义序列化程序无关。我认为问题出在您正在序列化的课程以及您如何创建它的实例,但是因为您没有像我要求的那样展示它我无法告诉您。

我能做的就是告诉你我是如何创建正确序列化的类的。

要序列化的类:

    public class Extra
    {
        public string code { get; set; }
        public object value { get; set; }
    }

    public class Product
    {
        public string sku { get; set; }
        public List<Extra> extras { get; set; }
    }

    public class RootObject
    {
        public Product product { get; set; }
    }

创建此类的实例:

var root = new RootObject()
{
    product = new Product()
    {
        sku = "211554",
        extras =  new List<Extra>()
        {
            new Extra()
            {
                code = "cost",
                value = "3.99"
            },
            new Extra()
            {
                code = "start_date",
                value = "2017-01-31T14:27:49.3032432+00:00"
            },
            new Extra()
            {
                code = "ids",
                value = new List<int> {1,2,3,19 }
            }
        }
    }
};

按照您已经显示的顺序对其进行序列化:

var json = JsonConvert.SerializeObject(root, _Settings);

json的输出:

{
"product":{
    "sku":"211554",
    "extras":[
    {
        "code":"cost",
        "value":"3.99"
    },
    {
        "code":"start_date",
        "value":"2017-01-31T14:27:49.3032432+00:00"
    },
    {
        "code":"ids",
        "value":[
            1,
            2,
            3,
            19
        ]
    }
    ]
}
}

答案 1 :(得分:0)

问题可能在于序列化类型的定义,特别是在&#34; extras&#34;属性是定义的。在没有看到该类型的情况下,我使用您发布的自定义序列化程序测试了以下内容,模拟了可能的实现:

public class Product
{
    public string SKU { get; set; }

    public ExtraInfo[] Extras { get; set; }

    public class ExtraInfo
    {
        public string Code { get; set; }
        public object Value { get; set; }
    }
}

要序列化的对象:

var product = new Product()
{
    SKU = "1234",
    Extras = new[]
    {
        new Product.ExtraInfo() { Code = "cost", Value = 3.99 },
        new Product.ExtraInfo() { Code = "ids", Value = new [] { 1, 2, 3 } }
    }
};

使用自定义序列化程序进行序列化:

var result = new CustomSerializer().Serialize(product);

结果JSON:

{  
   "SKU":"1234",
   "Extras":[  
      {  
         "Code":"cost",
         "Value":3.99
      },
      {  
         "Code":"ids",
         "Value":[  
            1,
            2,
            3
         ]
      }
   ]
}