反序列化json字符串给出的值不能为空.参数名称:s

本文关键字:参数 不能 字符串 json 反序列化 | 更新日期: 2023-09-27 18:10:59

"[{'"active'":true,'"campaignId'":11401,'"createdtime'":1355919181000,'"description'":'"Ankit Demo Edited By Nirav'",'"enddate'":1363132800000,'"groupId'":10179,'"isdeleted'":false,'"lastmodifiedby'":10405,'"modifiedtime'":1362556187000,'"name'":'"Ankit Demo'",'"noofweek'":12,'"organizationId'":11153,'"startdate'":1355875200000,'"status'":2,'"userId'":11161},
{'"active'":true,'"campaignId'":21901,'"createdtime'":1358493958000,'"description'":'"sdadadasd'",'"enddate'":1359072000000,'"groupId'":10179,'"isdeleted'":false,'"lastmodifiedby'":10405,'"modifiedtime'":1358751277000,'"name'":'"NEW CAMP TEST'",'"noofweek'":1,'"organizationId'":10707,'"startdate'":1358467200000,'"status'":4,'"userId'":10405},
{'"active'":true,'"campaignId'":33601,'"createdtime'":1361441582000,'"description'":'"dasdsadasd'",'"enddate'":1363219200000,'"groupId'":10179,'"isdeleted'":false,'"lastmodifiedby'":10405,'"modifiedtime'":1361795632000,'"name'":'"BHAVIK UTC'",'"noofweek'":3,'"organizationId'":10707,'"startdate'":1361404800000,'"status'":2,'"userId'":10405}]"

我有以上Jsong字符串在我的字符串变量..我想把这个json字符串转换成我的自定义类的对象。我创建了自定义类。以下是我从HttpWebResponse中获取json响应的代码,并在列表对象中转换我的自定义类

 HttpWebResponse response = default(HttpWebResponse);
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            //    response.Close()
            string sResult = null;
            using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
            {
                sResult = responseReader.ReadToEnd();
            }
            response.Close();
            Console.WriteLine(sResult);
  List<Class1> myDeserializedObjList = (List<Class1>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request[sResult], typeof(List<Class1>));
          int counts=  myDeserializedObjList.Count;
        }

但是它给我错误

Value cannot be null.
Parameter name: s
谁能告诉我怎么解决这个问题?

这是我的自定义类,我想从json字符串分配对象使用系统;使用System.Collections.Generic;使用来;使用包含;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    private Boolean active;
    public Boolean Active
    {
        get { return active; }
        set { active = value; }
    }
    private long campaignId;
    public long CampaignId
    {
        get { return campaignId; }
        set { campaignId = value; }
    }
    private long createdtime;
    public long Createdtime
    {
        get { return createdtime; }
        set { createdtime = value; }
    }
    private string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }
    private long enddate;
    public long Enddate
    {
        get { return enddate; }
        set { enddate = value; }
    }
    private long groupId;
    public long GroupId
    {
        get { return groupId; }
        set { groupId = value; }
    }
    private Boolean isdeleted;
    public Boolean Isdeleted
    {
        get { return isdeleted; }
        set { isdeleted = value; }
    }
    private long modifiedtime;
    public long Modifiedtime
    {
        get { return modifiedtime; }
        set { modifiedtime = value; }
    }
    private long lastmodifiedby;
    public long Lastmodifiedby
    {
        get { return lastmodifiedby; }
        set { lastmodifiedby = value; }
    }
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private int noofweek;
    public int Noofweek
    {
        get { return noofweek; }
        set { noofweek = value; }
    }
    private long organizationId;
    public long OrganizationId
    {
        get { return organizationId; }
        set { organizationId = value; }
    }
    private long startdate;
    public long Startdate
    {
        get { return startdate; }
        set { startdate = value; }
    }
    private Boolean status;
    public Boolean Status
    {
        get { return status; }
        set { status = value; }
    }
    private long userId;
    public long UserId
    {
        get { return userId; }
        set { userId = value; }
    }

    public Class1()
    {

    }
}

反序列化json字符串给出的值不能为空.参数名称:s

如果您的Request[sResult]是您提到的json字符串。试着

Newtonsoft.Json.JsonConvert.DeserializeObject<List<Class1>>(Request[sResult]);

编辑,我是说:

List<Class1> myDeserializedObjList =Newtonsoft.Json.JsonConvert.DeserializeObject<List<Class1>>(Request[sResult]);