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

时间:2013-10-04 10:19:30

标签: c# json json.net

我正在尝试反序列化这个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仍为空

3 个答案:

答案 0 :(得分:2)

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; }
}

(如果除了汽车之外没有任何东西,你可能可以摆脱中间物体。)

答案 1 :(得分:2)

如果你的课程是这样的话,你可以得到你需要的东西:

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

答案 2 :(得分:0)

参考以下代码片段希望它可以帮助你!!!

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();

    }
}
相关问题