Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将JSON反序列化为.NET时面临的问题_C#_.net_Json_Serialization_Javascriptserializer - Fatal编程技术网

C# 将JSON反序列化为.NET时面临的问题

C# 将JSON反序列化为.NET时面临的问题,c#,.net,json,serialization,javascriptserializer,C#,.net,Json,Serialization,Javascriptserializer,我对处理JSON比较陌生。但是我已经成功地创建了几个类,它们返回的正是我想要的。然而,这种反应让我感到困惑 我已经回顾了站点上的各种示例,并尝试将JSON响应转换为类。但是,我仍然会遇到运行时错误。 任何关于我错在哪里的指点都将不胜感激 JSON响应 { "locationResponse": { "locations": [ "E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:980

我对处理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;
}
public locationResponseResponse GetlocationResponseResponse(字符串jsonString)
{
locationResponseResponse_response=新的locationResponseResponse();
如果(!string.IsNullOrEmpty(jsonString))
{
JavaScriptSerializer ser=新的JavaScriptSerializer();
_response=ser.Deserialize(jsonString);
}
返回响应;
}
收到运行时错误


没有为
BlackFlagAPI.locationResponse[]

类型定义无参数构造函数,json示例中的
location
s是字符串,而不是json对象。因此json反序列化程序无法将它们转换为您的
location
类。字符串内容也不是序列化的json文档

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


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

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

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

public class Root
{
     public LocationResponse locationResponse { get; set; }
}
公共类定位响应
{
公共列表位置{get;set;}
}
公共类根
{
public LocationResponse LocationResponse{get;set;}
}

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

JSON中的位置
不是JSON对象。它们是格式为
name:value
的字符串,由
分隔

因此,您需要分两步对其进行解析

1-反序列化json

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

公共类位置
{
公共字符串E911AID{get;set;}
公共字符串streetDir{get;set;}
公共字符串street{get;set;}
公共字符串zip{get;set;}
公共字符串city{get;set;}
公共字符串streetNameSuffix{get;set;}
公共字符串名称{get;set;}
公共字符串状态{get;set;}
公共字符串houseNum{get;set;}
}
公共类定位响应
{
公共列表位置{get;set;}
}
公共类根对象
{
public LocationResponse LocationResponse{get;set;}
}

考虑使用Newtsoft.Json。它是最流行的json序列化程序。json示例中的
location
s是字符串,而不是json对象。因此json反序列化程序无法将它们转换为您的
location
类。您需要为此注册一个自定义解析器。下次发布json时,请使用打印精美的json。我修复了你的JSON,这个很好用:谢谢,经过进一步挖掘,我得到的JSON响应似乎有一个bug。我没有注意到几乎空的一行只包含一个ID。这似乎是问题的关键。一旦我删除了这行糟糕的代码,就连我的原始代码都能正常工作。现在,我需要确定JSON输出是否有缺陷,并可能修复内联错误或简单地出错。非常感谢所有的帮助。
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; }
}