将API响应转换为列表

时间:2016-03-11 09:59:09

标签: c# arrays json

很抱歉新手问题,但我如何采取类似的API响应 并仅使用地址

{
    "AccountId": 12345665555,
    "InvoicId": 1235,
    "Addresses":
    [["10 Watkin , , , , , Northampton, Northamptonshire"],
    ["12 Spencer Terrace, , , , , Northampton, Northamptonshire"],
    ["18 Watkin , , , , , Northampton, Northamptonshire"],
    ["22 Watkin , , , , , Northampton, Northamptonshire"]]
}

并将其转换为模型列表? 我尝试了这个,但不能把它放到列表中。

using (var client = new HttpClient())
{

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.GetAsync(addressRoute);



    var result = response.Result.Content.ReadAsStringAsync().Result;

    var objJavascript = new JavaScriptSerializer();
    AddressResult[] addressResult = objJavascript.Deserialize<AddressResult[]>(result);


}


private class AddressResult
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string Locality { get; set; }
    public string Town { get; set; }
    public string County { get; set; }

}

2 个答案:

答案 0 :(得分:1)

尝试这一点非常简单,只需使用Newtonsoft JsonConvert将json反序列化为对象。您的AddressResult类必须更改为:

var json = "{\"AccountId\": 12345665555, \"InvoicId\": 1235, \"Addresses\": [[\"10 Watkin , , , , , Northampton, Northamptonshire\"], [\"12 Spencer Terrace, , , , , Northampton, Northamptonshire\"], [\"18 Watkin , , , , , Northampton, Northamptonshire\"], [\"22 Watkin , , , , , Northampton, Northamptonshire\"]] }";
var addressList = JsonConvert.DeserializeObject<AddressResult>(json);

private class AddressResult
{
    public string AccountId { get; set; }
    public string InvoiceId { get; set; }
    public List<List<string>> Addresses { get; set; }           
}

答案 1 :(得分:0)

您可以使用JSON.NET反序列化所有响应并从反序列化对象获取地址。 要从json创建类,您可以使用json2csharp.com

class Program
{
    static void Main()
    {
        string json =
            "{ \"AccountId\": 12345665555, \"InvoicId\": 1235, \"Addresses\": [[\"10 Watkin , , , , , Northampton, Northamptonshire\"], [\"12 Spencer Terrace, , , , , Northampton, Northamptonshire\"], [\"18 Watkin , , , , , Northampton, Northamptonshire\"], [\"22 Watkin , , , , , Northampton, Northamptonshire\"]] }";

        RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

        foreach (List<string> address in root.Addresses)
        {
            string[] addressLines = address[0].Split(new char[] {','});

            AddressResult addressResult = new AddressResult()
            {
                Line1 = addressLines[0],
                Line2 = addressLines[1]
                //...
            };
        }

    }

    private class AddressResult
    {
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Line3 { get; set; }
        public string Locality { get; set; }
        public string Town { get; set; }
        public string County { get; set; }

    }

    public class RootObject
    {
        public long AccountId { get; set; }
        public int InvoicId { get; set; }
        public List<List<string>> Addresses { get; set; }
    }
}