面对将JSON反序列化为.NET的问题

时间:2015-02-21 09:01:34

标签: c# .net json serialization javascriptserializer

我对处理JSON相对较新。但是已经成功创建了几个可以完全返回我想要的类。但是,这种反应令我感到困惑。

我已经审查并尝试了网站上的各种示例,将我的JSON响应转换为类。但是,我继续遇到运行时错误。 关于我出错的地方的任何指示都将不胜感激。

JSON响应

{
    "locationResponse": {
        "locations": [
            "E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:Jonathon Doe,state:WA,houseNum:23619",
            "E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:Jon Doe,state:WA,houseNum:23619",
            "ad1c:2dbf:fadf:2e87",
            "E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:John Doe,state:WA,houseNum:23619",
            "E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:JJ Doe,state:WA,houseNum:23619"
        ]
    }
}

课程定义

[Serializable]
public class locationResponseResponse
{
    public locationResponse locationResponse { get; set; }
}
[Serializable]
public class locationResponse
{
    public location[] locations { get; set; }
}
[Serializable]
public class location
{
    public string E911AID { get; set; }
    public string streetDir { get; set; }
    public string street { get; set; }
    public string zip { get; set; }
    public string city { get; set; }
    public string streetNameSuffix { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public string houseNumber { get; set; }
}

反序列化代码段

public locationResponseResponse GetlocationResponseResponse(string jsonString)
{
    locationResponseResponse _response = new locationResponseResponse();
    if (!string.IsNullOrEmpty(jsonString))
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        _response = ser.Deserialize<locationResponseResponse>(jsonString);
    }
    return _response;
}

收到运行时错误

没有为BlackFlagAPI.locationResponse[]的类型定义无参数构造函数。

3 个答案:

答案 0 :(得分:2)

json示例中的location是字符串,而不是json对象。因此,json解串器无法将它们转换为location类。字符串内容也不是序列化的json文档。

您需要为此注册自定义解析器,但由于我从未使用过JavascriptSerializer,因此我无法告诉您如何操作。

或者,如果您可以控制json的源,则使用json编码位置数据,而不是在字符串中嵌入自定义格式。

答案 1 :(得分:0)

对于您发布的JSON,该类类似于以下内容:

public class LocationResponse
{
    public List<string> locations { get; set; }
}

public class Root
{
     public LocationResponse locationResponse { get; set; }
}

然后您需要手动解析位置。否则,您需要更正JSON结构。

答案 2 :(得分:0)

你的json中的

Locations不是json对象。它们是name:value形式的字符串,由,分隔。

所以你需要分两步解析它

1-反序列化json

var root = new JavaScriptSerializer().Deserialize<RootObject>(json);

2-解析每个字符串

var addrs = root.locationResponse.locations
                .Select(x => x.Split(','))
                .Select(parts =>
                {
                    var l = new Location();
                    var props = l.GetType().GetProperties();
                    foreach (var part in parts)
                    {
                        var kv = part.Split(new char[] { ':' }, 2);
                        var prop = l.GetType().GetProperty(kv[0]);
                        if(prop != null)
                            prop.SetValue(l, kv[1]);

                    }
                    return l;
                })
                .ToList();

public class Location
{
    public string E911AID { get; set; }
    public string streetDir { get; set; }
    public string street { get; set; }
    public string zip { get; set; }
    public string city { get; set; }
    public string streetNameSuffix { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public string houseNum { get; set; }
}

public class LocationResponse
{
    public List<string> locations { get; set; }
}

public class RootObject
{
    public LocationResponse locationResponse { get; set; }
}