[英]Need help deserializing JSON Object returned from API
I have the following JSON Output: 我有以下JSON输出:
{{
"$type": "Asi.Soa.Core.DataContracts.PagedResult`1[[Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts]], Asi.Contracts",
"Items": {
"$type": "System.Collections.Generic.List`1[[Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts]], mscorlib",
"$values": [
{
"$type": "Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts",
"EntityTypeName": "14",
"Properties": {
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyDataCollection, Asi.Contracts",
"$values": [
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "ResultRow",
"Value": "1"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Work Phone",
"Value": "(782) 438-7600"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Email",
"Value": "agsaz@rmax.net"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Full Name",
"Value": "Agazny"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "iMISId",
"Value": "eg1"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Preferred Phone",
"Value": "780"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Organization",
"Value": "Re"
}
]
}
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts",
"EntityTypeName": "14",
"Properties": {
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyDataCollection, Asi.Contracts",
"$values": [
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "ResultRow",
"Value": "2"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Work Phone",
"Value": "7802"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Email",
"Value": "aksm"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Full Name",
"Value": "Aji"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "iMISId",
"Value": "esa"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Preferred Phone",
"Value": "780"
},
{
"$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",
"Name": "Organization",
"Value": "Hom"
}
]
}
}
I am trying to deserialize this response into workable c# objects and POST it to a different API that accepts a totally different format of JSON. 我正在尝试将此响应反序列化为可行的c#对象,并将其发布到接受完全不同的JSON格式的其他API。
This is my code so far: 到目前为止,这是我的代码:
using (var client = new HttpClient())
{
// Format headers
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Request token, and append to headers
await AddTokenToHeaders(client);
// Query HTTP Service
var response = await client.GetAsync(baseUrl + "api/IQA?querydocumentversionkey=f2005a6e-7f47-47c3-a7e7-bbd2a7b6ab38");
if (response.IsSuccessStatusCode)
{
var customer = await response.Content.ReadAsStringAsync();
JObject result = JObject.Parse(await response.Content.ReadAsStringAsync());
JArray a = (JArray)result["Items"]["$values"][0]["Properties"];
var test = a.AsJEnumerable();
However, it's not picking up my entire JSON object and I am not able to map it to the properties of my class: 但是,它没有拾取我的整个JSON对象,因此无法将其映射到我的类的属性:
--Update. --update。
I have updated my code and I was able to get the Jarray in the format I wanted, however when I try enumerate by jObject into my List of Class Customer, the properties I try to cast returns nulls. 我已经更新了代码,并且能够以所需的格式获取Jarray,但是当我尝试通过jObject枚举到类客户列表中时,尝试转换的属性将返回null。
Thanks for the response. 感谢您的回复。 I was able to get the output I was looking for by using the following code. 通过使用以下代码,我可以获得所需的输出。 However, attempting to put the output in my List of Customer class with the properties specified however, I am getting nulls on the properties of class Customer. 但是,尝试将输出放入具有指定属性的“客户列表”类中,但是我在“客户”类的属性上得到了空值。
JObject result = JObject.Parse(await response.Content.ReadAsStringAsync());
JArray a = (JArray)result["Items"]["$values"];
List<Customer> items = ((JArray)a).Select(x => new Customer
{
ResultRow = (string)x["ResultRow"],
WorkPhone = (string)x["Work Phone"],
Email = (string)x["Email"],
FullName = (string)x["Full Name"],
iMISId = (string)x["iMISId"],
PreferredPhone = (string)x["Preferred Phone"],
Organization = (string)x["Organization"]
}).ToList();
public class Customer
{
[JsonProperty("ResultRow")]
public string ResultRow { get; set; }
[JsonProperty("Work Phone")]
public string WorkPhone { get; set; }
[JsonProperty("Email")]
public string Email { get; set; }
[JsonProperty("Full Name")]
public string FullName { get; set; }
[JsonProperty("iMISId")]
public string iMISId { get; set; }
[JsonProperty("Preferred Phone")]
public string PreferredPhone { get; set; }
[JsonProperty("Organization")]
public string Organization { get; set; }
}
There is a mistake in the code you provided. 您提供的代码有误。 You are trying to cast the Properties
into a JArray
when it is an object. 您正在尝试将Properties
转换为JArray
作为对象时。
If you do want the array in the Properties
object, do this: 如果确实要在Properties
对象中使用数组,请执行以下操作:
JArray arr = (JArray)result["Items"]["$values"][0]["Properties"]["$values"];
Otherwise, if you are looking for the Properties
object, then you should do this instead: 否则,如果您正在寻找“ Properties
对象,则应该这样做:
JObject obj = (JObject)result["Items"]["$values"][0]["Properties"];
Finally, to rebuild the list of Customer
instances, you could use the following logic with the static methods: 最后,要重建Customer
实例列表,可以将以下逻辑与静态方法结合使用:
var customersJson = (JArray)result["Items"]["$values"];
var customers = new List<Customer>();
foreach (JObject o in customersJson)
{
var customerJson = (JArray) o["Properties"]["$values"];
customers.Add(BuildCustomer(customerJson));
}
[...] [...]
private static Customer BuildCustomer(JArray a)
{
return new Customer
{
ResultRow = GetValue(a, "ResultRow"),
WorkPhone = GetValue(a, "Work Phone"),
Email = GetValue(a, "Email"),
FullName = GetValue(a, "Full Name"),
iMISId = GetValue(a, "iMISId"),
PreferredPhone = GetValue(a, "Preferred Phone"),
Organization = GetValue(a, "Organization")
};
}
private static string GetValue(JArray array, string name)
{
JToken obj = array.FirstOrDefault(x => (string) x["Name"] == name);
if (obj == null)
return string.Empty;
return (string) obj["Value"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.