Json.net c#将对象列表转换为2D数组

时间:2016-08-24 21:15:51

标签: c# arrays json.net converter

是否可以将对象列表从json转换为2D数组?

Data.json

{
    "x": 6,
    "y": 6,
    "information": [
        {
            "x": 0,
            "y": 0,
            "info": "First item",
            "info2": 1
        },
        {
            "x": 1,
            "y": 3,
            "info": "Second item",
            "info2": 3
        },
        {
            "x": 3,
            "y": 4,
            "info": "Third item",
            "info2": 2
        }
    ]
}

第一个x和y是2D数组的大小。是否可以使用Custom JsonConverter根据信息对象的x和y将信息列表放入此数组? 我知道有可能首先将它转换为一个列表,然后将Json.net转换为数组,但是在进行deseriliazing时它是否可能?

2 个答案:

答案 0 :(得分:1)

我为您创建了一个自定义转换器:

波索:

public class Item
{
    [JsonProperty("x")]
    public int X { get; set; }

    [JsonProperty("y")]
    public int Y { get; set; }

    [JsonProperty("info")]
    public string Info { get; set; }

    [JsonProperty("info2")]
    public string Info2 { get; set; }
}

转换器:

public class ItemJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Item[,]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jObject = JObject.Load(reader);

        var x = jObject.Value<int>("x");
        var y = jObject.Value<int>("y");

        var items = jObject.GetValue("information").ToObject<IEnumerable<Item>>();
        var itemsArray = new Item[x, y];

        foreach (var item in items)
        {
            itemsArray[item.X, item.Y] = item;
        }

        return itemsArray;
    }

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

用法:

var jsonStr = @"{
                    ""x"": 3,
                    ""y"": 1,
                    ""information"": [
                        {
                            ""x"": 0,
                            ""y"": 0,
                            ""info"": ""First item"",
                            ""info2"": 1
                        },
                        {
                            ""x"": 1,
                            ""y"": 0,
                            ""info"": ""Second item"",
                            ""info2"": 3
                        },
                        {
                            ""x"": 2,
                            ""y"": 0,
                            ""info"": ""Third item"",
                            ""info2"": 2
                        }
                    ]
                }";

var jss = new JsonSerializerSettings();
jss.Converters.Add(new ItemJsonConverter());
var obj = JsonConvert.DeserializeObject<Item[,]>(jsonStr, jss);

答案 1 :(得分:0)

也许最简单/最快的方法是手动完成,如下所示:

using Newtonsoft.Json;

namespace WpfApplication3
{
    public partial class MainWindow
    {
        private readonly string json = @"
{
    ""x"": 6,
    ""y"": 6,
    ""information"": [
        {
            ""x"": 0,
            ""y"": 0,
            ""info"": ""First item"",
            ""info2"": 1
        },
        {
            ""x"": 1,
            ""y"": 3,
            ""info"": ""Second item"",
            ""info2"": 3
        },
        {
            ""x"": 3,
            ""y"": 4,
            ""info"": ""Third item"",
            ""info2"": 2
        }
    ]
}";

        public MainWindow()
        {
            InitializeComponent();

            var o = JsonConvert.DeserializeObject<SampleResponse1>(json);
            var array = new IInformation[o.X, o.Y];
            foreach (var i in o.Information)
            {
                array[i.X, i.Y] = i;
            }
        }
    }

    internal class SampleResponse1
    {
        [JsonProperty("x")]
        public int X { get; set; }

        [JsonProperty("y")]
        public int Y { get; set; }

        [JsonProperty("information")]
        public Information[] Information { get; set; }
    }

    internal class Information : IInformation
    {
        [JsonProperty("x")]
        public int X { get; set; }

        [JsonProperty("y")]
        public int Y { get; set; }

        #region IInformation Members

        [JsonProperty("info")]
        public string Info { get; set; }

        [JsonProperty("info2")]
        public int Info2 { get; set; }

        #endregion
    }

    internal interface IInformation
    {
        string Info { get; set; }
        int Info2 { get; set; }
    }
}

请注意,我并没有真正打扰,只是使用了隐藏 xy的界面,随时可以进一步调整它。

另外,我使用http://jsonclassgenerator.codeplex.com/转换为类。