反序列化包含另一个对象的 json 对象

本文关键字:json 对象 一个对象 包含另 反序列化 | 更新日期: 2023-09-27 18:34:05

我正在尝试反序列化此json文件:

{
   "result":
     {
        "car1":{"lat":37.989728,"long":23.664633},
        "car2":{"lat":38.008027,"long":23.774068}
     }
}

我试过这样:

public static void parseJson(string data)
{
     Result all = JsonConvert.DeserializeObject<Result>(data);  
}
public class Result
{
     public Car Car { get; set; }
}
public class Car
{
     public string lat { get; set; }
     public string lon { get; set; }
}

但对象all仍然为空

反序列化包含另一个对象的 json 对象

如果你像这样制作你的类,你可以得到你需要的东西:

class Result
{
    [JsonProperty("result")]
    public Dictionary<string, Car> Cars { get; set; }
}
class Car
{
    public decimal Lat { get; set; }
    public decimal Long { get; set; }
}

下面是一个示例程序,演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
           ""result"":
             {
                ""car1"":{""lat"":37.989728,""long"":23.664633},
                ""car2"":{""lat"":38.008027,""long"":23.774068}
             }
        }";
        Result result = JsonConvert.DeserializeObject<Result>(json);
        foreach (KeyValuePair<string, Car> kvp in result.Cars)
        {
            Console.WriteLine(kvp.Key + ": lat=" + kvp.Value.Lat + 
                                        ", long=" + kvp.Value.Long);
        }
    }
}

这是上述内容的输出:

car1: lat=37.989728, long=23.664633
car2: lat=38.008027, long=23.774068

JSON 中的对象有两个属性,car1car2 ,因此您要将其映射到的类 ( Result ) 应该具有这两个属性:

public class Result
{
     public Car car1 { get; set; }
     public Car car2 { get; set; }
}

在下面重新发表您的评论:

好吧,但那两辆车就是一个例子。实际上,每次我获取json时,汽车的数量都会有所不同

在这种情况下,JSON 必须更改为使用数组:

{
   "result":
     {
        cars: [
            {"lat":37.989728,"long":23.664633},
            {"lat":38.008027,"long":23.774068}
        ]
     }
}

然后我认为Result类应该是:

public class Result
{
     public List<Car> cars { get; set; }
}

或可能

public class Result
{
     public Car[] cars { get; set; }
}

(如果你除了汽车之外什么都没有,你也许可以摆脱中间物体。

参考 下面的代码片段 希望它可以帮助您!!

class WeapsCollection
{
    public Dictionary<string, WeaponDetails> Weapons { get; set; }
}
class WeaponList
{
    public WeaponDetails AEK { get; set; }
    public WeaponDetails XM8 { get; set; }
}
class WeaponDetails
{
    public string Name { get; set; }
    public int Kills { get; set; }
    public int Shots_Fired { get; set; }
    public int Shots_Hit { get; set; }
}
class Program  
{
    static void Main(string[] args)
    {
        string json = @"
        {
           'weapons':
                    {
                       'aek':
                            {
                               'name':'AEK-971 Vintovka',
                               'kills':47,
                               'shots_fired':5406,
                               'shots_hit':858
                            },
                       'xm8':
                            {
                               'name':'XM8 Prototype',
                               'kills':133,
                               'shots_fired':10170,
                               'shots_hit':1790
                            },
                    }
        }";
        WeapsCollection weps = JsonConvert.DeserializeObject<WeapsCollection>(json);
        Console.WriteLine(weps.Weapons.First().Value.Shots_Fired);            
        Console.ReadLine();
    }
}