gpt4 book ai didi

c# - 使用 HttpClient 从 Web API 反序列化 Json

转载 作者:行者123 更新时间:2023-11-30 23:09:23 26 4
gpt4 key购买 nike

我从 WebAPI 返回了以下 Json 格式。你们能帮忙反序列化吗?

{
"@odata.context":"http://....... ","value":[
**{
"RecordNumber":"LDxxxx","RecordType":"Loan","PropertyAddress":{ "Address1":"601 xxxx","Address2":null,"Zip":"99999","City":"abc","State":"ab","County":"abcd" }
,"Summary":{ "BorrowerName":null,"ProductCode":null,"Status":"Not Registered" }
}**,{
"RecordNumber":"LDxxxx","RecordType":"Loan","PropertyAddress":{ "Address1":"601 xxxx","Address2":null,"Zip":"99999","City":"abc","State":"ab","County":"abcd" }
,"Summary":{ "BorrowerName":null,"ProductCode":null,"Status":"Not Registered" }
},
….]
}

我需要 value 元素中的内容。粗体是 API 返回中重复的内容。我创建了一个符合如下描述的类。

public class RootObject
{
[JsonProperty(PropertyName = "RecordNumber")]
public string RecordNumber { get; set; }

[JsonProperty(PropertyName = "RecordType")]
public string RecordType { get; set; }

[JsonProperty(PropertyName = "PropertyAddress")]
public PropertyAddress PropertyAddress { get; set; }

[JsonProperty(PropertyName = "Summary")]
public Summary Summary { get; set; }
}

能够使用以下方法跳过 Json 数组中的第一条记录,得到“值”部分....但未能成功检索“值”对象

var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(forecast);
foreach (var kv in dict.Skip(1))
{
JArray jsonVal = JArray.Parse(kv.Value.ToString());
}

感谢您的帮助。

萨提亚

最佳答案

您可以反序列化为具体类(在 http://json2csharp.com/ 的帮助下)

var result = JsonConvert.DeserializeObject<SOTest.Result>(json);

public class SOTest
{
public class PropertyAddress
{
public string Address1 { get; set; }
public object Address2 { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string State { get; set; }
public string County { get; set; }
}

public class Summary
{
public object BorrowerName { get; set; }
public object ProductCode { get; set; }
public string Status { get; set; }
}

public class Value
{
public string RecordNumber { get; set; }
public string RecordType { get; set; }
public PropertyAddress PropertyAddress { get; set; }
public Summary Summary { get; set; }
}

public class Result
{
[JsonProperty("@odata.context")]
public string Context { get; set; }
public List<Value> Value { get; set; }
}
}

关于c# - 使用 HttpClient 从 Web API 反序列化 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45846317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com