使用可变键反序列化JSON

时间:2019-05-06 13:40:11

标签: c# json deserialization

具有此json格式数据:

{
    "43296001103418946331805279000": {
      "city": "43296001",
      "district": "43296001-1034189463",
      "floor": "5",
      "price": "279000",
      "rooms": "3",
      "size": "80"
    },
      "432960018694019521321777312000": {
      "city": "43296001",
      "district": "43296001-86940195213",
      "floor": "7",
      "price": "312000",
      "rooms": "2",
      "size": "77"
    }
}

我上了这个课:

public class Flat
{
  public string city{ get; set; }
  public string district{ get; set; }
  public string floor{ get; set; }
  public string price{ get; set; }
  public string rooms{ get; set; }
  public string size{ get; set; }
}

但是,如果键是变量id,我如何反序列化对象?

2 个答案:

答案 0 :(得分:1)

在这种情况下,您将:

public class JsonData
{
    public List<Dictionary<string, Flat>> Data { get; set; }
}

然后反序列化此操作:

JsonData data = JsonConvert.DeserializeObject<JsonData>(json);

答案 1 :(得分:1)

我认为您需要使用这种方式:

var out = JsonConvert.DeserializeObject<Dictionary<string, ISBN>>(json);        
var isbn_out = out.Values.First();

然后将其用于:

 isbn_out.Flat.city;

看看这个similar example