How to convert HttpResponseMessage having OData to

2019-07-27 18:50发布

问题:

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.

回答1:

you should do it like this -

public class Value
{
    [JsonProperty("@odata.etag")]
    public string etag { get; set; }
    public string accountnumber { get; set; }
    public string accountid { get; set; }
}

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

then deserialize-

var value = JsonConvert.DeserializeObject<RootObject>(json);


回答2:

We can parse and create Anonymous Type based on that. In your case, replace the Anonymous Type with Account object.

Given the JSON string:

string json = @"{
   '@odata.context':'https://APIURL/api/data/v8.1/$metadata#account(accountnumber)',
   'value':[
      {
         '@odata.etag':'W/\'12496866\'',
         'accountnumber':'D00208',
         'accountid':'30417c0f-7b8c-e611-80f3-5065f38bd4d1'
      }
   ]
}";

It can be parsed as below:

var jsonObject = JObject.Parse(json);
var dataObject = new
{
    Context = jsonObject["@odata.context"],
    Values = jsonObject["value"].AsEnumerable<JToken>()
                                .Select(v => new
                                {
                                    ETag = v["@odata.etag"],
                                    AccountNumber = v["accountnumber"],
                                    AccountId = v["accountid"]
                                }).ToArray()
};

In order to convert to Account object where the object is defined as below:

public class Account
{
    public string Number { get; set; }
    public string Id { get; set; }
}

Then the JSON object can be parsed as below (if looking for only first node; It can also be converted to list of Accounts:

var jsonObject = JObject.Parse(json);
var account = jsonObject["value"].AsEnumerable<JToken>()
                                 .Select(v => new Account()
                                 {
                                     Number = v["accountnumber"].ToString(),
                                     Id = v["accountid"].ToString()
                                 }).FirstOrDefault();


回答3:

You can generalize the accepted answer by using a generic class to deserialize json web response:

     class RootObject<T>
     {
        public List<T> Value { get; set; }
     }

     var odata  = JsonConvert.DeserializeObject<RootObject<POCO>>(json);

Try it with live Demo