JArray到属性对象

时间:2017-10-15 12:32:23

标签: c# json

我需要将以下JArray转换为Object。 我的问题是,我需要使用Attributes来做到这一点。 因为我无法在解耦的类中更改以下调用:

var message = JsonConvert.DeserializeObject(e.Message, messageType)

JSON

[71,[[77372266,1508067366000,-0.12144759,5517.4],[77372265,1508067366000,-0.020001,5518.2],[77372251,1508067362000,-0.01,5517.2],[77372235,1508067358000,0.020001,5517.8],[77372220,1508067352000,-0.41883579,5517.3],[77372218,1508067352000,-1.05740887,5517.2],[77372211,1508067351000,-1.8084,5517.2],[77372207,1508067350000,-0.01565918,5517.2],[77372206,1508067349000,-0.01721768,5514.3],[77372205,1508067349000,-0.10347625,5514.3],[77372201,1508067348000,-1.08183286,5513.1],[77372200,1508067348000,-0.04218858,5513.1],[77372199,1508067348000,-0.020001,5513.3],[77372198,1508067348000,-0.020001,5513.3],[77372197,1508067348000,-0.020001,5513.3],[77372196,1508067348000,-0.020001,5513.3],[77372195,1508067348000,-2.16396207,5513.3],[77372194,1508067348000,-1.5601,5513.5],[77372193,1508067348000,-0.13520397,5514.2],[77372192,1508067347000,-0.01479603,5514.2],[77372191,1508067347000,-0.13659854,5513.4],[77372189,1508067346000,-0.01597635,5513.3],[77372187,1508067346000,-0.19812221,5513.3],[77372184,1508067346000,-0.01261482,5513.3],[77372183,1508067346000,0.131033,5519.7],[77372182,1508067346000,0.2011,5519.4],[77372181,1508067346000,0.074337,5518.2],[77372180,1508067346000,0.125,5517.5],[77372179,1508067346000,0.06401,5516.5],[77372178,1508067346000,0.05305,5516.3]]]

我的课程

public class Root 
{
    public int ChannelId { get; set; }

    public List<Item> Data { get; set; }

}

public class Item
{
    public int Id { get; set; }
    public long Timestamp { get; set; }

    public decimal Price { get; set; }
    public decimal Size { get; set; }
}

2 个答案:

答案 0 :(得分:2)

如果您使用Newtonsoft JSON library(现在称为JSON.NET),您可以使用Jsonconverter class创建自己的自定义转换器。

你的课程看起来像是:

 public class MyCustomConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(Root).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.StartArray)
            {
                JArray rootToken = JArray.Load(reader);

                if (rootToken[0] != null && rootToken[1] != null)
                {
                    var root = new Root(rootToken[0].Value<int>(), this.readItems(rootToken[1]));
                    return root;
                }
            }

            return existingValue;
        }

        private IList<Item> readItems(JToken items)
        {
            var itemList = new List<Item>();
            if (items.Type == JTokenType.Array)
            {
                foreach(var item in items.Children())
                {
                    if (item.Type == JTokenType.Array && item.Count() == 4)
                    {
                        itemList.Add(new Item(
                            item[0].Value<int>(),
                            item[1].Value<long>(),
                            item[2].Value<decimal>(),
                            item[3].Value<decimal>()));
                    }
                }
            }

            return itemList;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

请注意,当前状态下的自定义转换器只能将JSON转换为对象,如果您希望它能够将Root对象转换为JSON字符串,则需要实现{{1方法自己。

你说由于依赖关系的解耦,你无法改变被调用者调用deserialize方法的方式。您可以做的是覆盖JSONConvert.DefaultSettings属性并明确告诉它使用自定义转换器。

WriteJson

答案 1 :(得分:1)

首先将json解析为JArray,然后只需创建一个函数将其解析为您选择的对象。你可以试试这个:

string json = "[71,[[77372266,1508067366000,-0.12144759,5517.4],[77372265,1508067366000,-0.020001,5518.2],[77372251,1508067362000,-0.01,5517.2],[77372235,1508067358000,0.020001,5517.8],[77372220,1508067352000,-0.41883579,5517.3],[77372218,1508067352000,-1.05740887,5517.2],[77372211,1508067351000,-1.8084,5517.2],[77372207,1508067350000,-0.01565918,5517.2],[77372206,1508067349000,-0.01721768,5514.3],[77372205,1508067349000,-0.10347625,5514.3],[77372201,1508067348000,-1.08183286,5513.1],[77372200,1508067348000,-0.04218858,5513.1],[77372199,1508067348000,-0.020001,5513.3],[77372198,1508067348000,-0.020001,5513.3],[77372197,1508067348000,-0.020001,5513.3],[77372196,1508067348000,-0.020001,5513.3],[77372195,1508067348000,-2.16396207,5513.3],[77372194,1508067348000,-1.5601,5513.5],[77372193,1508067348000,-0.13520397,5514.2],[77372192,1508067347000,-0.01479603,5514.2],[77372191,1508067347000,-0.13659854,5513.4],[77372189,1508067346000,-0.01597635,5513.3],[77372187,1508067346000,-0.19812221,5513.3],[77372184,1508067346000,-0.01261482,5513.3],[77372183,1508067346000,0.131033,5519.7],[77372182,1508067346000,0.2011,5519.4],[77372181,1508067346000,0.074337,5518.2],[77372180,1508067346000,0.125,5517.5],[77372179,1508067346000,0.06401,5516.5],[77372178,1508067346000,0.05305,5516.3]]]";

var array = JArray.Parse(json);

var channelId = array[0];
var listData = array[1].ToList();

Root root = new Root();
root.ChannelId = Convert.ToInt32(channelId);
root.Data = new List<Item>();
for (int i = 0; i < listData.Count; i++)
{
    Item newItem = new Item();
    var item = listData[i].ToList();

    newItem.Id = Convert.ToInt32(item[0]);
    newItem.Timestamp = Convert.ToInt64(item[1]);
    newItem.Price = Convert.ToDecimal(item[2]);
    newItem.Size = Convert.ToDecimal(item[3]);
    root.Data.Add(newItem);
}