Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用动态键反序列化对象集合_C#_Asp.net_Json_Json.net - Fatal编程技术网

C# 使用动态键反序列化对象集合

C# 使用动态键反序列化对象集合,c#,asp.net,json,json.net,C#,Asp.net,Json,Json.net,我的应用程序正在使用API,我正在尝试反序列化返回的数据。数据的格式如下: { "1000!%abc":{ "listingID":"1000" "zipcode":"87654", "address":"123 Main St", "streetNumber":"123", "streetName":"Main St", "latitude":-22.04666 "longitude":-32.65537, }, "2000

我的应用程序正在使用API,我正在尝试反序列化返回的数据。数据的格式如下:

{
  "1000!%abc":{
    "listingID":"1000"
    "zipcode":"87654",
    "address":"123 Main St",
    "streetNumber":"123",
    "streetName":"Main St",
    "latitude":-22.04666 
    "longitude":-32.65537,
  },
  "2000!%abc":{
    "listingID":"2000"
    "zipcode":"45678",
    "address":"345 Main St",
    "streetNumber":"345",
    "streetName":"Main St",
    "latitude":-22.04666 
    "longitude":-32.65537,
  }
}
我有以下模型课程:

public class PropertyListViewModel
{
    public List<PropertyViewModel> Properties { get; set; }
}

public class PropertyViewModel
{
    [JsonProperty("listingID")]
    public int ListingId { get; set; }
}
公共类PropertyListViewModel
{
公共列表属性{get;set;}
}
公共类PropertyViewModel
{
[JsonProperty(“listingID”)]
public int ListingId{get;set;}
}
我现在只是想获取listingID,以确保它正常工作

... // create HttpClient object, add headers and such
System.Net.Http.HttpResponseMessage response = await client.GetAsync(endpointUrl);
var jsonString = response.Content.ReadAsStringAsync();
PropertyListViewModel model = 
    JsonConvert.DeserializeObject<PropertyListViewModel>(jsonString.Result);
..//创建HttpClient对象,添加头等等
System.Net.Http.httpresponsemessageresponse=wait client.GetAsync(endpointUrl);
var jsonString=response.Content.ReadAsStringAsync();
PropertyListViewModel模型=
反序列化对象(jsonString.Result);
但是
model
总是返回空值,所以它不能正确地进行反序列化

有没有办法更改视图模型,以便正确地反序列化json?

使用
字典来表示属性列表模型

假设

public class PropertyViewModel {
    public string ListingID { get; set; }
    public string Zipcode { get; set; }
    public string Address { get; set; }
    public string StreetNumber { get; set; }
    public string StreetName { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}
从那里

var response = await client.GetAsync(endpointUrl);
var jsonString = await response.Content.ReadAsStringAsync();
var propertyList = JsonConvert.DeserializeObject<Dictionary<string, PropertyViewModel>>(jsonString);
var property = propertyList["1000!%abc"];
var response=wait client.GetAsync(endpointUrl);
var jsonString=await response.Content.ReadAsStringAsync();
var propertyList=JsonConvert.DeserializeObject(jsonString);
var property=propertyList[“1000!%abc”];

请注意,提供的示例JSON格式不好,因为缺少逗号。

使用
字典
2000!%abc
不是
列表的有效索引。