【发布时间】:2020-07-20 21:19:14
【问题描述】:
我有一个 web api,它返回一个对象类型的数组。 Web api 工作正常。当我尝试将对象反序列化为对象类型的数组时,我只会得到空值。我错过了什么?
json:
{
"results":"[{\"Name\":\"Rocky\",\"Breed\":\"Pitbull\",\"Color\":\"Brown\",\"Weight\":\"76\",\"OwnerUserId\":null,\"FamilyId\":\"1006949\"},{\"Name\":\"Casper\",\"Breed\":\"Terrier \",\"Color\":\"White \",\"Weight\":\"15\",\"OwnerUserId\":null,\"FamilyId\":\"1006949\"}]"
}
反序列化代码:
public async Task values()
{
var AJson = new root();
var client = http.CreateClient();
var response = await client.GetAsync("https://doggoapi2020.azurewebsites.net/Doggies/1006949");
var responsebody = await response.Content.ReadAsStringAsync();
AJson = Newtonsoft.Json.JsonConvert.DeserializeObject<root>(responsebody);
}
型号:
public class DoggoData
{
[JsonProperty(PropertyName = "Name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "Breed")]
public string Breed { get; set; }
public string Color { get; set; }
public string Weight { get; set; }
public string OwnerUserId { get; set; }
public string FamilyId { get; set; }
}
public class root
{
public DoggoData[] Jsonres { get; set; }
}
【问题讨论】:
-
我认为JSON arrays应该不用引号,in your case
-
我用牛顿序列化器来序列化数据...
标签: c# json rest asp.net-core