使用HttpClient从WebAPI反序列化Json [英] Deseriaize Json from WebAPI using HttpClient

查看:156
本文介绍了使用HttpClient从WebAPI反序列化Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following Json format returned from a WebAPI. Can you guys help to deserialize please?.

{
  "@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返回的内容中重复的内容.我创建了一个符合以下描述的类.

I need what's in the value element. The bold is what is repeated in the return from API. I created a class that matches the description as below.

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数组中的第一条记录,获得了值"部分....但是未能成功检索值"对象

Was able to skip the first record in the Json array using the following, got the "Value" part....but have not been successful in retrieving the "Value" object

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/)

You can deserialize to concrete classes (with the help of 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; }
    }
}

这篇关于使用HttpClient从WebAPI反序列化Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆