0

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

4

3 回答 3

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

(如果除了汽车之外没有其他任何东西,您也许可以摆脱中间对象。)

于 2013-10-04T10:23:48.743 回答
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
于 2013-10-04T14:59:56.920 回答
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();

    }
}
于 2013-10-04T12:16:41.267 回答