[英]Custom json converter json.net
I'm trying to deserialize a complex JSON object and the "JsonSerializer().Deserialize" dosent work well on this specific object, Link to this problem . 我正在尝试反序列化复杂的JSON对象,并且“ JsonSerializer()。Deserialize”代理在此特定对象上工作得很好,请链接到此问题 。 Now I'm trying to do custom Deserialization and i'm stuck since i'm not familiar with C# or JSON format. 现在,我正在尝试执行自定义反序列化,并且由于我对C#或JSON格式不熟悉而陷入困境。 please help me. 请帮我。 Example of the JSON data: 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
}
]}
EDIT: Here's an example of the JSON data I get from the server 编辑:这是我从服务器获取的JSON数据的示例
and a real text file for example: 和一个真实的文本文件,例如:
{"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}]} {“ stationary_obstacles”:[{“纬度”:20.0,“ cylinder_height”:4323.0,“ cylinder_radius”:345.0,“经度”:20.0}],“ moving_obstacless”:[{“ 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,“经度”:0.0}]}}
I already answered to this question here . 我已经在这里回答了这个问题。 Now you just changed some values in your JSON from int to float. 现在,您只需将JSON中的一些值从int更改为float。 I will paste code from my existing answer with small changes. 我将对现有答案中的代码进行小的更改即可粘贴。
EDIT : You do not need to create you models manually. 编辑 :您不需要手动创建模型。 There are many options to generate them based on your JSON. 有很多选项可根据您的JSON生成它们。 For example: 例如:
Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes
it will generate class structure for you. 如果将JSON复制到剪贴板,然后在Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes
将Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes
,它将为您生成类结构。 These models are generated using json2csharp.com : 这些模型是使用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; }
}
Deserialization using JSON.NET: 使用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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.