Json.net自定义子对象转换器
本文关键字:对象 转换器 自定义 net Json | 更新日期: 2023-09-27 18:04:28
可能这与c# json.net自定义的子对象序列化有关。但是没有帮助……
我想在解析时将List反序列化为数据[,],但转换器不希望工作。data[,]是类的成员。我是否需要将转换器添加到JsonProperty?我真的需要两个转换器吗?
此刻我得到了这个:
反序列化多维数组时意外标记:StartObject。路径'[0]',第2行,位置3.
搜索没有帮助:/
Program.cs
using System;
using Newtonsoft.Json;
using System.IO;
public class Program {
static void Main(string[] args) {
JsonSerializerSettings jss = new JsonSerializerSettings();
jss.Converters.Add(new MyObjectJsonConverter());
MyObject field = JsonConvert.DeserializeObject<MyObject>(File.ReadAllText("data.json").ToString(), jss);
Console.ReadLine();
}
}
MyObject.cs
using Newtonsoft.Json;
public class MyObject {
[JsonProperty(PropertyName = "area", Required = Required.Always)]
public area m_area;
[JsonProperty(PropertyName = "datalist", Required = Required.Always)]
public data[,] m_datalist;
}
data.cs
using Newtonsoft.Json;
public class data {
[JsonProperty(PropertyName = "info", Required = Required.Always)]
public string m_info;
[JsonProperty(PropertyName = "info2", Required = Required.Always)]
public string m_info2;
}
area.cs
using Newtonsoft.Json;
public class area {
[JsonProperty(PropertyName = "x", Required = Required.Always)]
public int m_x;
[JsonProperty(PropertyName = "y", Required = Required.Always)]
public int m_y;
}
和两个转换器。
MyObjectJsonConverter.cs
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class MyObjectJsonConverter : JsonConverter {
public override bool CanConvert(Type objectType) {
return objectType == typeof(MyObject);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
JObject jObject = JObject.Load(reader);
MyObject result = new MyObject();
result.m_area = JsonConvert.DeserializeObject<area>(jObject["area"].ToString());
JsonSerializerSettings jss = new JsonSerializerSettings();
jss.Converters.Add(new DataListJsonConverter(result.m_area.m_x, result.m_area.m_y));
result.m_datalist = JsonConvert.DeserializeObject<data[,]>(jObject["datalist"].ToString(), jss);
return result;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
throw new NotImplementedException();
}
}
DataListJsonConverter.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
public class DataListJsonConverter : JsonConverter {
private readonly int m_x;
private readonly int m_y;
public DataListJsonConverter(int x, int y) {
m_x = x;
m_y = y;
}
public override bool CanConvert(Type objectType) {
return objectType == typeof(List<data>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
JObject jObject = JObject.Load(reader);
data[,] tilesArray = new data[m_x, m_y];
foreach (JToken token in jObject["datalist"]) {
data curTile = JsonConvert.DeserializeObject<data>(token.ToString());
tilesArray[(int) token["x"], (int) token["y"]] = curTile;
}
return tilesArray;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
throw new NotImplementedException();
}
}
和data.json
{
"area": {
"x": 10,
"y": 10
},
"datalist": [
{
"x": 1,
"y": 2,
"info": "test1",
"info2": "test1"
},
{
"x": 3,
"y": 5,
"info": "test2",
"info2": "test2"
},
{
"x": 5,
"y": 7,
"info": "test3",
"info2": "test3"
},
{
"x": 6,
"y": 4,
"info": "test4",
"info2": "test4"
},
]
}
对DataListJsonConvert做一些小改动可能会达到这个效果:
更新can转换方法以检查您试图反序列化到
的内容。public override bool CanConvert( Type objectType )
{
return objectType == typeof( data[,] );
}
和在ReadJson实现中将json内容读取为JArray而不是JObject
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
{
JArray jArray = JArray.Load( reader );
data[,] tilesArray = new data[m_x, m_y];
foreach( JToken token in jArray )
{
data curTile = JsonConvert.DeserializeObject<data>( token.ToString() );
tilesArray[(int) token["x"], (int) token["y"]] = curTile;
}
return tilesArray;
}
,我认为这应该给你多维数组结构,你正在寻找