I am calling a REST service from my C# application which connects to CRM. This returns HttpResponseMessage.
response.Content.ReadAsStringAsync().Result
The above statement returns following output. I need to convert this to Account object, which already has "accountnumber, and accountid properties.
{
"@odata.context":"https://APIURL/api/data/v8.1/$metadata#account(accountnumber)","value":[ { "@odata.etag":"W/\"12496866\"","accountnumber":"D00208","accountid":"30417c0f-7b8c-e611-80f3-5065f38bd4d1" } ] }
I have tried following code
Account return = JsonConvert.DeserializeObject<Account>(response.Content.ReadAsStringAsync().Result);
But this doesn't fill up the object, and it always has null values in accountnumber, and accountid fields.
Any idea of how to properly convert this response to the C# type.
You can generalize the accepted answer by using a generic class to deserialize json web response:
Try it with live Demo
We can parse and create
Anonymous Type
based on that. In your case, replace theAnonymous Type
withAccount
object.Given the JSON string:
It can be parsed as below:
In order to convert to
Account
object where the object is defined as below:Then the
JSON
object can be parsed as below (if looking for only first node; It can also be converted to list ofAccount
s:you should do it like this -
then deserialize-