自定义 json 转换器 json.net
Custom json converter json.net
我正在尝试反序列化一个复杂的 JSON 对象,并且 "JsonSerializer().Deserialize" dosent 在这个特定对象 Link to this problem 上运行良好。现在我正在尝试进行自定义反序列化,但由于我不熟悉 C# 或 JSON 格式,所以卡住了。
请帮我。
JSON 数据示例:
{"stationary_osbtacles": [
{
"latitude": 33.83320,
"longitude": 33.83320,
"cylinder_radius": 33.83320,
"cylinder_height": 33.83320
}
],
"moving_obstacles": [
{
"latitude": 33.83320,
"longitude": 33.83320,
"altitude_msl": 33.83320,
"sphere_radius": 33.83320
}
]}
编辑: 这是我从服务器获取的 JSON 数据的示例
和一个真实的文本文件,例如:
{"stationary_obstacles": [{"latitude": 20.0, "cylinder_height": 4323.0, "cylinder_radius": 345.0, "longitude": 20.0}], "moving_obstacles": [{"latitude": 20.0, "sphere_radius": 50.0, "altitude_msl": 100.0, "longitude": 20.0}, {"latitude": 35.0, "sphere_radius":453.0,"altitude_msl":45345.0,"longitude":35.0},{"latitude":0.0,"sphere_radius":3242.0,"altitude_msl":0.14,"longitude": 0.0}]}
我已经回答了这个问题here。现在您只是将 JSON 中的一些值从 int 更改为 float。我将粘贴我现有答案中的代码,稍作改动。
编辑:您不需要手动创建模型。有很多选项可以根据您的 JSON 生成它们。例如:
- Visual Studio。如果您将 JSON 复制到剪贴板并在
Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes
中,它将为您生成 class 结构。
- json2csharp.com。用于从中生成 classes 的基于 Web 的工具
JSON.
这些模型是使用 json2csharp.com:
生成的
public class StationaryObstacle
{
public double latitude { get; set; }
public double cylinder_height { get; set; }
public double cylinder_radius { get; set; }
public double longitude { get; set; }
}
public class MovingObstacle
{
public double latitude { get; set; }
public double sphere_radius { get; set; }
public double altitude_msl { get; set; }
public double longitude { get; set; }
}
public class RootObject
{
public List<StationaryObstacle> stationary_obstacles { get; set; }
public List<MovingObstacle> moving_obstacles { get; set; }
}
反序列化使用JSON.NET:
var json = "{\"stationary_obstacles\":[{\"latitude\":20.0,\"cylinder_height\":4323.0,\"cylinder_radius\":345.0,\"longitude\":20.0}],\"moving_obstacles\":[{\"latitude\":20.0,\"sphere_radius\":50.0,\"altitude_msl\":100.0,\"longitude\":20.0},{\"latitude\":35.0,\"sphere_radius\":453.0,\"altitude_msl\":45345.0,\"longitude\":35.0},{\"latitude\":0.0,\"sphere_radius\":3242.0,\"altitude_msl\":0.14,\"longitude\":0.0}]}";
// Option 1
var d1 = JsonConvert.DeserializeObject<RootObject>(json);
// Option 2
using (var stringReader = new StringReader(json))
{
var d2 = (RootObject)new JsonSerializer().Deserialize(stringReader, typeof(RootObject));
}
我正在尝试反序列化一个复杂的 JSON 对象,并且 "JsonSerializer().Deserialize" dosent 在这个特定对象 Link to this problem 上运行良好。现在我正在尝试进行自定义反序列化,但由于我不熟悉 C# 或 JSON 格式,所以卡住了。 请帮我。 JSON 数据示例:
{"stationary_osbtacles": [
{
"latitude": 33.83320,
"longitude": 33.83320,
"cylinder_radius": 33.83320,
"cylinder_height": 33.83320
}
],
"moving_obstacles": [
{
"latitude": 33.83320,
"longitude": 33.83320,
"altitude_msl": 33.83320,
"sphere_radius": 33.83320
}
]}
编辑: 这是我从服务器获取的 JSON 数据的示例
和一个真实的文本文件,例如:
{"stationary_obstacles": [{"latitude": 20.0, "cylinder_height": 4323.0, "cylinder_radius": 345.0, "longitude": 20.0}], "moving_obstacles": [{"latitude": 20.0, "sphere_radius": 50.0, "altitude_msl": 100.0, "longitude": 20.0}, {"latitude": 35.0, "sphere_radius":453.0,"altitude_msl":45345.0,"longitude":35.0},{"latitude":0.0,"sphere_radius":3242.0,"altitude_msl":0.14,"longitude": 0.0}]}
我已经回答了这个问题here。现在您只是将 JSON 中的一些值从 int 更改为 float。我将粘贴我现有答案中的代码,稍作改动。
编辑:您不需要手动创建模型。有很多选项可以根据您的 JSON 生成它们。例如:
- Visual Studio。如果您将 JSON 复制到剪贴板并在
Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes
中,它将为您生成 class 结构。 - json2csharp.com。用于从中生成 classes 的基于 Web 的工具 JSON.
这些模型是使用 json2csharp.com:
生成的public class StationaryObstacle
{
public double latitude { get; set; }
public double cylinder_height { get; set; }
public double cylinder_radius { get; set; }
public double longitude { get; set; }
}
public class MovingObstacle
{
public double latitude { get; set; }
public double sphere_radius { get; set; }
public double altitude_msl { get; set; }
public double longitude { get; set; }
}
public class RootObject
{
public List<StationaryObstacle> stationary_obstacles { get; set; }
public List<MovingObstacle> moving_obstacles { get; set; }
}
反序列化使用JSON.NET:
var json = "{\"stationary_obstacles\":[{\"latitude\":20.0,\"cylinder_height\":4323.0,\"cylinder_radius\":345.0,\"longitude\":20.0}],\"moving_obstacles\":[{\"latitude\":20.0,\"sphere_radius\":50.0,\"altitude_msl\":100.0,\"longitude\":20.0},{\"latitude\":35.0,\"sphere_radius\":453.0,\"altitude_msl\":45345.0,\"longitude\":35.0},{\"latitude\":0.0,\"sphere_radius\":3242.0,\"altitude_msl\":0.14,\"longitude\":0.0}]}";
// Option 1
var d1 = JsonConvert.DeserializeObject<RootObject>(json);
// Option 2
using (var stringReader = new StringReader(json))
{
var d2 = (RootObject)new JsonSerializer().Deserialize(stringReader, typeof(RootObject));
}