2

我不确定如何定义data属性以反序列化以下 json 数据...

{
"response": {
    "total_rows": 39,
    "data": [
      [
        "XXXXXXXXXXXXXXXXXBs2VI",
        "a7ac4aa7-7211-4116-be57-0c36fc2abeee",
        "AAAAAA",
        "Crn Burnley & Victoria St",
        "Richmond",
        "VIC",
        "3121",
        "555-555-555",
        null,
        "Real Estate & Home Improvement > Storage",
        null,
        null,
        -37.8114511488511,
        145.009782837163
      ],
      [ .. ]
      [ .. ]
      ....
    },
    status = "ok"
}

所以我有..

public class ResultData
{
    public Response Response { get; set; }
    public string Status { get; set; }
}

和....

public class Response
{
    public int total_rows { get; set; }
    public IList<string> data { get; set; }
}

但这抛出了一个异常,说"Can't cast float to string". 所以它试图将最后两个浮点数添加到List<>.. 但失败了。

请问我该如何解决这个问题?(我无法控制 json 输出)。

干杯:)

更新

看起来datajson 属性的内容是具有固定对象类型的固定大小数组。那么有没有一种方法可以在我的 .NET 代码中定义它,以便 JSON.NET 知道如何准确地反序列化它?我是否需要为该 json 属性制作自定义 json 反序列化器?

4

4 回答 4

3

如果您使用的是 .NET 4.0,那么您可能想dynamic在处理 JSON 时考虑使用。

我发布了一些代码,显示了在另一个问题上设置它的一种方法。

在您的示例中,您可以执行以下操作:

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });

dynamic obj = serializer.Deserialize(json, typeof(object));

if (obj.status != "ok")
    throw new OopsException();

int rowCount = obj.total_rows;

foreach (var row in obj.data) {
    string code1 = row[0];
    string code2 = row[1];
    string code3 = row[2];
    string address = row[3];
    string suburb = row[4];
    string state = row[5];
    string postcode = row[6];
    string phone = row[7];
    string something1 = row[8];
    string name = row[9];
    string something2 = row[10];
    string something3 = row[11];
    decimal longitude = row[12];
    decimal latitude = row[13];

    // TODO use above values
}
于 2011-02-21T12:35:36.880 回答
1

您的数组中有字符串和数字。您必须使用object而不是string具有列表类型。

public class Response
{
    public int total_rows { get; set; }
    public IList<object> data { get; set; }
}
于 2011-02-18T14:28:17.800 回答
1

我不确定这是否是最好的方法,但我通过这样做使它工作:

public class Response
{
    [JsonProperty(PropertyName = "total_rows")]
    public int TotalRows { get; set; }
    public IList<MyPoco> Data { get; set; }
}

现在,数据属性不会被 JSON 库自动填充,这 100% 没问题。

然后我这样做:

RestResponse<ResultData> response = client.Execute<ResultData>(restRequest);

ResultData data = response.Data;

if (data != null)
{
    // Now the Data property will be null, so we need to parse that badboy.
    JObject linqData = JObject.Parse(response.Content);
    var array = linqData["response"]["data"] as JArray;
    if (array != null)
    {
        data.Response.Data = 
            (from x in array
            select new MyPoco
                        {
                            SubjectKey = x[0].Value<string>(),
                            UUid = x[1].Value<string>(),
                            Name = x[2].Value<string>(),
                            Address = x[3].Value<string>(),
                            City = x[4].Value<string>(),
                            Territory = x[5].Value<string>(),
                            Postcode = x[6].Value<string>(),
                            Telephone = x[7].Value<string>(),
                            Category = x[9].Value<string>(),
                            Website = x[10].Value<string>(),
                            Email = x[11].Value<string>(),
                            Latitude = x[12].Value<float>(),
                            Longitude = x[13].Value<float>()
                        }).ToList();
    }
}

因此,我不会让 JSON 库自动提取该列表对象列表,但我使用一些 Linq-To-Json 并提取那些坏男孩。

不完全高兴,但它有效。

于 2011-02-21T12:28:56.227 回答
0

看起来你有一个列表列表。在这种情况下,它应该是:

public class Response
{
    public int total_rows { get; set; }
    public IList<IList<object>> data { get; set; }
}
于 2011-02-19T12:42:39.670 回答