反序列化json字符串赋值不能为null。参数名称:s

时间:2013-04-25 18:33:23

标签: c# asp.net json deserialization json.net

"[{\"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;     使用System.Linq;     使用System.Web;

/// <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()
    {


    }
}

1 个答案:

答案 0 :(得分:-1)

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

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

编辑,我的意思是:

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