在C#中反序列化嵌套JSON数组的问题

时间:2015-02-21 23:45:50

标签: c# json json.net restsharp

修改 我注意到这是负面的投票,我想这是因为没有展示研究成果。 我看了很多堆栈溢出帖子。我找不到一个JSON这个重度嵌套的例子,这个例子是用JSON中存储的数据后来被操作的例子来访问的。

问题和目标

使用restsharp我收到了对我所做的API调用的JSON响应,但是我正在努力反序列化我得到的响应。我想要使​​用的数据似乎是一个嵌套数组(下图)。

我的目标是然后将该数组的内容传递给变量,检查它是否已填充,获取该数组的第一项,然后将该项中的属性设置为等于我数据库中的对象。

这是我使用当前代码获取的错误消息。看起来我将Hit对象视为错误类型的对象然而,在将我的头撞到墙壁上几个小时后,我不完全确定为什么特别是这对于这个特定的JSON结构不起作用。

当前代码

                    var hitresult = JsonConvert.DeserializeObject( response.Content, typeof( List<Hit> ) ) as List<Hit>;
                    if (hitresult.Any())
                    {
                            var address = hitresult.FirstOrDefault();
                            verified = true;
                            result = string.Format( "UDPRN: {0}", address.udprn ); 
                            location.Street1 = address.line_1;
                            location.Street2 = address.line_2;
                            location.City = address.post_town;
                            location.State = address.county;
                            location.PostalCode = address.postcode;

错误消息

  

“Newtonsoft.Json.JsonSerializationException”类型的例外   发生在Newtonsoft.Json.dll但未在用户代码中处理

     

其他信息:无法反序列化当前的JSON对象   (例如{“name”:“value”})进入类型   'System.Collections.Generic.List`1 [org.hopecorby.LocationService.Hit]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。

     

要修复此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为正常的.NET   type(例如,不是像整数这样的基本类型,不是集合类型   像数组或List一样,可以从JSON对象反序列化。   JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。

     

路径'结果',第1行,第10位。

示例JSON

 {
    "result": {
        "total": 2,
        "limit": 10,
        "page": 0,
        "hits": [
            {
                "dependant_locality": "",
                "postcode_type": "L",
                "po_box": "",
                "post_town": "LONDON",
                "delivery_point_suffix": "1A",
                "double_dependant_locality": "",
                "su_organisation_indicator": " ",
                "longitude": -0.127695242183412,
                "department_name": "",
                "district": "Westminster",
                "building_name": "",
                "dependant_thoroughfare": "",
                "northings": 179951,
                "premise": "10",
                "postcode_outward": "SW1A",
                "postcode_inward": "2AA",
                "sub_building_name": "",
                "eastings": 530047,
                "postcode": "SW1A 2AA",
                "country": "England",
                "udprn": 23747771,
                "line_3": "",
                "organisation_name": "Prime Minister & First Lord Of The Treasury",
                "ward": "St James's",
                "county": "",
                "line_1": "Prime Minister & First Lord Of The Treasury",
                "building_number": "10",
                "thoroughfare": "Downing Street",
                "line_2": "10 Downing Street",
                "latitude": 51.5035398826274
            },
            {
                "dependant_locality": "",
                "postcode_type": "S",
                "po_box": "",
                "post_town": "LONDON",
                "delivery_point_suffix": "1B",
                "double_dependant_locality": "",
                "su_organisation_indicator": " ",
                "longitude": -0.122624730080001,
                "department_name": "",
                "district": "Camden",
                "building_name": "Downing Court",
                "dependant_thoroughfare": "",
                "northings": 182178,
                "premise": "Flat 10, Downing Court",
                "postcode_outward": "WC1N",
                "postcode_inward": "1LX",
                "sub_building_name": "Flat 10",
                "eastings": 530342,
                "postcode": "WC1N 1LX",
                "country": "England",
                "udprn": 26245117,
                "line_3": "Grenville Street",
                "organisation_name": "",
                "ward": "Bloomsbury",
                "county": "",
                "line_1": "Flat 10",
                "building_number": " ",
                "thoroughfare": "Grenville Street",
                "line_2": "Downing Court",
                "latitude": 51.5234851731108
            }
        ]
    },
    "code": 2000,
    "message": "Success"
}

我的模型(使用json2charp创建)

public class Hit
{
    public string dependant_locality { get; set; }
    public string postcode_type { get; set; }
    public string po_box { get; set; }
    public string post_town { get; set; }
    public string delivery_point_suffix { get; set; }
    public string double_dependant_locality { get; set; }
    public string su_organisation_indicator { get; set; }
    public double longitude { get; set; }
    public string department_name { get; set; }
    public string district { get; set; }
    public string building_name { get; set; }
    public string dependant_thoroughfare { get; set; }
    public int northings { get; set; }
    public string premise { get; set; }
    public string postcode_outward { get; set; }
    public string postcode_inward { get; set; }
    public string sub_building_name { get; set; }
    public int eastings { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
    public int udprn { get; set; }
    public string line_3 { get; set; }
    public string organisation_name { get; set; }
    public string ward { get; set; }
    public string county { get; set; }
    public string line_1 { get; set; }
    public string building_number { get; set; }
    public string thoroughfare { get; set; }
    public string line_2 { get; set; }
    public double latitude { get; set; }
}

public class Result
{
    public int total { get; set; }
    public int limit { get; set; }
    public int page { get; set; }
    public List<Hit> hits { get; set; }
}

public class RootObject
{
    public Result result { get; set; }
    public int code { get; set; }
    public string message { get; set; }
}

1 个答案:

答案 0 :(得分:3)

Deserializer需要一个JSON数组。您的JSON是包含JSON数组的JSON对象。解串器无法知道您希望它开始使用hits数组。

您需要反序列化为RootObject。然后,您就可以将List<Hit>称为结果的属性。

<强>更新

以下代码应该让您了解我的意思。我测试了这个,它适用于我,你的对象和你的JSON。

var sr = new StreamReader(@"C:\Users\danielc\Documents\Visual Studio 2012\Projects\TestJSON\TestJSON\response.json");
string json = sr.ReadToEnd();
sr.Close();

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

if (hits.Any())
{
    var address = hits.FirstOrDefault();
    var udprn = string.Format("UDPRN: {0}", address.udprn);
    Console.WriteLine(udprn);
}

Console.Read();