Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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反序列化错误:参数不能为null_C#_Json_Serialization_Deserialization - Fatal编程技术网

C# JSON反序列化错误:参数不能为null

C# JSON反序列化错误:参数不能为null,c#,json,serialization,deserialization,C#,Json,Serialization,Deserialization,无法从其他Json序列化问题中找到答案,因此可能有人可以帮助我: 我从RESTAPI获取一个JSON对象,并尝试将其反序列化为一个对象。下面是我收到的JSON对象: {"id":"6wVcZ9ZF67ECUQ8xuIjFT2", "userId":"83ca0ab5-3b7c-48fe-8019-000320081b00", "authorizations":["employee","API","trainer","queueAdmin","supervisor","workflowAdmin"

无法从其他Json序列化问题中找到答案,因此可能有人可以帮助我:

我从RESTAPI获取一个JSON对象,并尝试将其反序列化为一个对象。下面是我收到的JSON对象:

{"id":"6wVcZ9ZF67ECUQ8xuIjFT2",
"userId":"83ca0ab5-3b7c-48fe-8019-000320081b00",
"authorizations":["employee","API","trainer","queueAdmin","supervisor","workflowAdmin","realtimeManager","forecastAnalyst","qualityEvaluator","contactCenterManager","teamLead","personnelAdmin","telephonyAdmin","qualityAdmin","businessAdmin","businessUser","accountAdmin","dialerAdmin","contentManagementUser","contentManagementAdmin","admin","api","scriptDesigner","agent","user"],
"primaryAuthorization":"employee",
"thirdPartyOrgName":"in",
"username":"somebody",
"selfUri":"https://blahblahblah.com/api/v1/auth/sessions/6wVcZ9ZF67ECUQ8xuIjFT2"}
我试图反序列化的对象:

 [Serializable]
public class Session : BaseRequest, ISession
{
    public Session(string url) : base(url)
    {
    }

    #region Members

    [JsonProperty(PropertyName = "userId")]
    public string UserId { get; set; }

    [JsonProperty(PropertyName = "authorizations")]
    public object[] Authorizations { get; set; }

    [JsonProperty(PropertyName = "primaryAuthorization")]
    public string PrimaryAuthorization { get; set; }

    [JsonProperty(PropertyName = "thirdPartyOrgName")]
    public string ThirdPartyOrgName { get; set; }

    [JsonProperty(PropertyName = "username")]
    public string Username { get; set; }

    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "selfUri")]
    public string SelfUri { get; set; }

    #endregion
}
我只是使用流读取器发出web请求并获取响应流,然后返回字符串。相当标准

但是,当我尝试反序列化到会话对象时,它总是抛出一个错误:值不能为Null

var serializer = new JsonSerializer();
response = MakePostRequest(true);
var obj = serializer.Deserialize<Session>(new JsonTextReader(new StringReader(response)));
我有什么指示可以阻止它这样做吗?或者我需要围绕这一点进行重构吗?

下面的代码可以工作(使用):


但我认为这不是一个好的方法。更改您的设计可能是更好的解决方案

我已经测试了您的代码,它运行良好,我所做的唯一更改是删除构造函数,我认为序列化程序由于某种原因无法在对象上创建实例,您可以删除吗

public Session(string url) : base(url)
{
}

您的代码对我来说很好,但我没有BaseRequest源代码,所以我用空构造函数创建了类

依我看,例外情况正是从这里开始的。在会话构造函数中,url参数为null,因为JSON对象没有url属性。可能在BaseRequest类中,您使用此url参数并收到值不能为Null错误

如果出现以下问题,您可以仅更改参数的名称:

  public Session(string selfUri ) : base(selfUri)
  {
  }

还要检查“response”变量是否为null。如果将null传递给其构造函数,StringReader可以抛出此异常。

我想我终于看到我的问题了。我应该如何告诉它忽略基类?@Encryption我不知道什么是
BaseRequest
。如果你把它贴出来,我可以试着帮你。把我的基础课上的东西贴出来谢谢。绝对没有想到,所以我继续做了一个轻微的重构。每当我将它序列化为Json时,我就意识到所有被拉入的对象……嗯,你的代码对我来说工作得很好。只需给我们完整的异常消息/堆栈跟踪。您确定响应不是空的吗?如果将null传递给StringReader构造函数,则可以获得此异常。另一个选项是来自BaseRequest构造函数的异常。如果
selfUri
是OP希望在基类中使用的url,那么这就是最好的答案。(那样的话,我就把我的拿走)
public class Session
{
    public string Id { get; set; }
    public string UserId { get; set; }
    public List<string> Authorizations { get; set; }
    public string PrimaryAuthorization { get; set; }
    public string ThirdPartyOrgName { get; set; }
    public string Username { get; set; }
    public string SelfUri { get; set; }
}
var session = (Session)System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(typeof(Session));
JsonConvert.PopulateObject(DATA, session);
public Session(string url) : base(url)
{
}
  public Session(string selfUri ) : base(selfUri)
  {
  }