我可以将 JSON 数组反序列化为 class 的属性吗?
Can I deserialize a JSON array into properties of a class?
我有一个 JSON 文件包含这样的行:
{"id":"0258","name":"Canterbury","coordinates":[1.07992,51.27904]}
coordinates
项是城市的地理坐标。但是,它存储为一个数组,其中始终包含 [longitude, latitude].
我想将这些条目反序列化为如下定义的 C# POCO classes:
public sealed class City
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("coordinates")]
public GeographicLocation Location { get; set; }
}
public sealed class GeographicLocation
{
public GeographicLocation(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public double Latitude { get; private set; }
public double Longitude { get; private set; }
}
我想你能明白我的意思。
是否有注释这些 class 的方法,以便源 JSON 将正确反序列化为我想要的对象图?
我目前正在使用 System.Text.Json
,但如果有更好的解决方案,我会立即切换到 Newtonsoft.Json
。
澄清
我知道如何将 JSON 反序列化为 class,其中 JSON 元素和 class 属性之间存在对应关系。这里的基本问题是源 JSON 文件包含一个 2 元素数组,我希望它在反序列化 class 的属性中结束,而不是在单个数组 属性 中.我不知道该怎么做。
如上所述,您可以使用自定义 JsonConverter<>。一个简单的解决方案如下所示:
public class GeographicLocationJsonConverter : JsonConverter<GeographicLocation>
{
public override GeographicLocation ReadJson(JsonReader reader, Type objectType, [AllowNull] GeographicLocation existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var token = JArray.Load(reader);
var values = token.ToObject<List<double>>();
return new GeographicLocation(values[0], values[1]);
}
public override void WriteJson(JsonWriter writer, [AllowNull] GeographicLocation value, Newtonsoft.Json.JsonSerializer serializer)
{
var jArray = new JArray(new double[] { value.Latitude, value.Longitude });
jArray.WriteTo(writer);
}
}
用法:
var city = new City()
{
Location = new GeographicLocation(1, 2),
Name = "Zhytomyr"
};
var converter = new GeographicLocationJsonConverter();
var json = JsonConvert.SerializeObject(city, converter);
var deserializedCity = JsonConvert.DeserializeObject<City>(json, converter);
我有一个 JSON 文件包含这样的行:
{"id":"0258","name":"Canterbury","coordinates":[1.07992,51.27904]}
coordinates
项是城市的地理坐标。但是,它存储为一个数组,其中始终包含 [longitude, latitude].
我想将这些条目反序列化为如下定义的 C# POCO classes:
public sealed class City
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("coordinates")]
public GeographicLocation Location { get; set; }
}
public sealed class GeographicLocation
{
public GeographicLocation(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public double Latitude { get; private set; }
public double Longitude { get; private set; }
}
我想你能明白我的意思。
是否有注释这些 class 的方法,以便源 JSON 将正确反序列化为我想要的对象图?
我目前正在使用 System.Text.Json
,但如果有更好的解决方案,我会立即切换到 Newtonsoft.Json
。
澄清
我知道如何将 JSON 反序列化为 class,其中 JSON 元素和 class 属性之间存在对应关系。这里的基本问题是源 JSON 文件包含一个 2 元素数组,我希望它在反序列化 class 的属性中结束,而不是在单个数组 属性 中.我不知道该怎么做。
如上所述,您可以使用自定义 JsonConverter<>。一个简单的解决方案如下所示:
public class GeographicLocationJsonConverter : JsonConverter<GeographicLocation>
{
public override GeographicLocation ReadJson(JsonReader reader, Type objectType, [AllowNull] GeographicLocation existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var token = JArray.Load(reader);
var values = token.ToObject<List<double>>();
return new GeographicLocation(values[0], values[1]);
}
public override void WriteJson(JsonWriter writer, [AllowNull] GeographicLocation value, Newtonsoft.Json.JsonSerializer serializer)
{
var jArray = new JArray(new double[] { value.Latitude, value.Longitude });
jArray.WriteTo(writer);
}
}
用法:
var city = new City()
{
Location = new GeographicLocation(1, 2),
Name = "Zhytomyr"
};
var converter = new GeographicLocationJsonConverter();
var json = JsonConvert.SerializeObject(city, converter);
var deserializedCity = JsonConvert.DeserializeObject<City>(json, converter);