[英]C# Rest Response in JSON Deserialize to Custom Object
I have reviewed a lot of posts about this question and I am still having trouble. 我已经审查了很多有关此问题的帖子,但仍然遇到麻烦。 I am using Newtonsoft.Json and the following code: 我正在使用Newtonsoft.Json和以下代码:
string url = @"https://https://someSite.com/indexes('myIndex')search=SEARCH_PHONES:";
string phoneNumber = "5550005555";
url += phoneNumber;
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.Method = "GET";
request.Headers.Add("api-key", "####");
WebResponse response = request.GetResponse();
Stream imageStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(imageStream, encode);
string s = readStream.ReadToEnd();
JObject joResponse = JObject.Parse(s);
JArray array = (JArray)joResponse["value"];
string customer = array[0].ToString();
The problem is that array is returning as one long string. 问题在于数组将作为一个长字符串返回。 How do I access the individual key/value pairs? 如何访问单个键/值对? Below is the response I recieve: 以下是我收到的回复:
{
"@odata.context": "https://someSite.com/indexes('myIndex')/$metadata#docs",
"value": [
{
"@search.score": 10.933167,
"CUSTOMER_DIM_ID": "77049309",
"SOURCE_CUSTOMER_ID": null,
"PORTSTORE_ID": "0326_1448401",
"FIRST_NM": "First Name",
"LAST_NM": "Last Name",
"ADDR_LINE_1": "133 Main St",
"ADDR_LINE_2": null,
"CITY": "MyCity",
"STATE_CD": "IL",
"POSTAL_CD": "99999",
"COUNTRY": "United States",
"EMAIL_ADDR": "myEmail@gmail.com",
"BIRTH_DT": null,
"PHONE": "5550005555",
"GENDER_CD": "F",
"SEARCH_EMAILS": [
"myEmail@gmail.com"
],
"SEARCH_PHONES": [
"5550005555"
],
"JUS_EMAIL_OPTIN": true,
"JCA_EMAIL_OPTIN": true,
"LCA_EMAIL_OPTIN": true,
"JUS_DM_OPTIN": true,
"JCA_DM_OPTIN": true,
"LCA_DM_OPTIN": true,
"MOBILE_OPTIN": false,
"LIFETIME_REVENUE": "138.1800",
"LIFETIME_UNITS": 7,
"NTH_ORDER": 2,
"FIRST_PURCHASE_DT": "2016-02-11T00:00:00Z",
"LAST_PURCHASE_DT": "2016-02-19T00:00:00Z",
"AVG_LAG": 8,
"IsDeleted": false,
"UPDATE_DT": "2016-02-19T00:00:00Z"
}
]
} }
I don't have access to change the response. 我无权更改响应。 I tried to use json2sharp to create the objects and then simply deserialize but it said that the "@search.score" was invalid as well as "@odata.context". 我试图使用json2sharp创建对象,然后简单地反序列化,但是它说“ @ search.score”和“ @ odata.context”都是无效的。 After I commented out those lines in my C# code it does not deserialize correctly (everything is null) I need to be able to retrieve the customer information and assign it to my custom class. 在我用C#代码注释掉这些行之后,它没有正确反序列化(所有内容都为null),我需要能够检索客户信息并将其分配给我的自定义类。
It looks like you're doing a little extra work here. 您似乎在这里做了一些额外的工作。 Instead of 代替
Stream imageStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(imageStream, encode);
string s = readStream.ReadToEnd();
Do string s = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
做string s = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Then, you can do a JsonConvert.DeserializeObject<MyType>(s)
and that should get you your object. 然后,您可以执行JsonConvert.DeserializeObject<MyType>(s)
,这应该为您提供对象。
Also in your object, make sure to use the appropriate serialization attributes wherever your names don't match up, if you wanted a particular naming style for example. 同样,在对象中,请确保在名称不匹配的地方使用适当的序列化属性 ,例如,如果要使用特定的命名样式。
Don't manually parse through JSON unless you have a really good reason to do so. 除非有充分的理由,否则请不要通过JSON手动解析。
Make a web request using your rest client of choice (postman/dhcp/etc.) and copy the response. 使用您选择的其他客户端(postman / dhcp / etc。)发出Web请求,然后复制响应。 Then create a new .cs file and go to Edit -> Past Special -> Paste Json as class. 然后创建一个新的.cs文件,并转到Edit-> Past Special-> Paste Json作为类。
From here you should get a mostly working POCO that can be used for deserializing the JSON using Newtonsoft.Json.JsonConvert.Deserialize<Class>(responseJson)
. 从这里,您应该获得一个可以正常工作的POCO,可以使用Newtonsoft.Json.JsonConvert.Deserialize<Class>(responseJson)
将JSON反序列化。
Note that newtonsoft defaults to camel case for json properties. 请注意,对于json属性,newtonsoft默认为驼峰式。 Since your properties are not camel case, you need to explicitly define the property name. 由于属性不是驼峰大小写,因此您需要显式定义属性名称。
[JsonProperty("ID")]
public int ID { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.