C# 将JSON反序列化到列表时出现问题<;T>;

C# 将JSON反序列化到列表时出现问题<;T>;,c#,json,deserialization,C#,Json,Deserialization,将JSON字符串反序列化到列表时遇到问题 该项目如下: [JsonObject(MemberSerialization.OptIn)] public class TCProject { public override string ToString() { return Name; } [JsonProperty(PropertyName = "archived")] pub

将JSON字符串反序列化到列表时遇到问题

该项目如下:

[JsonObject(MemberSerialization.OptIn)]
    public class TCProject
    {
        public override string ToString()
        {
            return Name;
        }

        [JsonProperty(PropertyName = "archived")]
        public bool Archived { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string Description { get; set; }

        [JsonProperty(PropertyName = "href")]
        public string Href { get; set; }

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

        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "webUrl")]
        public string WebUrl { get; set; }
    }
{"project":[{"name":"GCUK","id":"project11","href":"/httpAuth/app/rest/projects/id:project11"},{"name":"Interiors In Spain","id":"project3","href":"/httpAuth/app/rest/projects/id:project3"}]}
public IEnumerable<TCProject> GetAllProjects()
        {
            var uri = _connection.CreateUri("/httpAuth/app/rest/projects");
            var request = _connection.Request(uri);

            var projects = JsonConvert.DeserializeObject<List<TCProject>>(request);

    return projects;
}
JSON字符串如下所示:

[JsonObject(MemberSerialization.OptIn)]
    public class TCProject
    {
        public override string ToString()
        {
            return Name;
        }

        [JsonProperty(PropertyName = "archived")]
        public bool Archived { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string Description { get; set; }

        [JsonProperty(PropertyName = "href")]
        public string Href { get; set; }

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

        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "webUrl")]
        public string WebUrl { get; set; }
    }
{"project":[{"name":"GCUK","id":"project11","href":"/httpAuth/app/rest/projects/id:project11"},{"name":"Interiors In Spain","id":"project3","href":"/httpAuth/app/rest/projects/id:project3"}]}
public IEnumerable<TCProject> GetAllProjects()
        {
            var uri = _connection.CreateUri("/httpAuth/app/rest/projects");
            var request = _connection.Request(uri);

            var projects = JsonConvert.DeserializeObject<List<TCProject>>(request);

    return projects;
}
转换字符串的代码如下所示:

[JsonObject(MemberSerialization.OptIn)]
    public class TCProject
    {
        public override string ToString()
        {
            return Name;
        }

        [JsonProperty(PropertyName = "archived")]
        public bool Archived { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string Description { get; set; }

        [JsonProperty(PropertyName = "href")]
        public string Href { get; set; }

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

        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "webUrl")]
        public string WebUrl { get; set; }
    }
{"project":[{"name":"GCUK","id":"project11","href":"/httpAuth/app/rest/projects/id:project11"},{"name":"Interiors In Spain","id":"project3","href":"/httpAuth/app/rest/projects/id:project3"}]}
public IEnumerable<TCProject> GetAllProjects()
        {
            var uri = _connection.CreateUri("/httpAuth/app/rest/projects");
            var request = _connection.Request(uri);

            var projects = JsonConvert.DeserializeObject<List<TCProject>>(request);

    return projects;
}
public IEnumerable GetAllProjects()
{
var uri=_connection.CreateUri(“/httpAuth/app/rest/projects”);
var request=\u connection.request(uri);
var projects=JsonConvert.DeserializeObject(请求);
返回项目;
}
我得到的例外情况是:

Newtonsoft.Json.JSONSerializationException:{“无法将Json对象反序列化为'System.Collections.Generic.List`1[TCProject]'类型。”


一定有一些我遗漏了的非常简单的东西-有人有什么想法吗?

我认为在这种情况下,您只需要获取JSON的数组部分即可反序列化到如下列表中:

    public IEnumerable<TCProject> GetAllProjects()
    {
        var uri = _connection.CreateUri("/httpAuth/app/rest/projects");
        var request = _connection.Request(uri);

        var projects = JsonConvert.DeserializeObject<List<TCProject>>(request.Substring(11, request.Length - 1));

        return projects;
    }
public IEnumerable GetAllProjects()
{
var uri=_connection.CreateUri(“/httpAuth/app/rest/projects”);
var request=\u connection.request(uri);
var projects=JsonConvert.DeserializeObject(request.Substring(11,request.Length-1));
返回项目;
}

我很确定,如果您创建了一个名为project的类,该类具有一个属性,该属性是一个列表,并反序列化到该对象,那么一切都会正常工作

//Using a page "test.aspx" in my existing project (I already had it open)
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = "{\"project\":[{\"name\":\"GCUK\",\"id\":\"project11\",\"href\":\"/httpAuth/app/rest/projects/id:project11\"},{\"name\":\"Interiors In Spain\",\"id\":\"project3\",\"href\":\"/httpAuth/app/rest/projects/id:project3\"}]}";
        var p = JsonConvert.DeserializeObject<TCProjectWrapper>( s );
        s = "this"; //for easy breakpointing
    }
}
[JsonObject( MemberSerialization.OptIn )]
public class TCProjectWrapper {
    [JsonProperty( PropertyName = "project" )]
    private List<TCProject> Project { get; set; }
}
[JsonObject( MemberSerialization.OptIn )]
public class TCProject {
    public override string ToString() {
        return Name;
    }

    [JsonProperty( PropertyName = "archived" )]
    public bool Archived { get; set; }

    [JsonProperty( PropertyName = "description" )]
    public string Description { get; set; }

    [JsonProperty( PropertyName = "href" )]
    public string Href { get; set; }

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

    [JsonProperty( PropertyName = "name" )]
    public string Name { get; set; }

    [JsonProperty( PropertyName = "webUrl" )]
    public string WebUrl { get; set; }
}
//在现有项目中使用“test.aspx”页面(我已经打开了它)
使用制度;
使用System.Collections.Generic;
使用Newtonsoft.Json;
公共部分类测试:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
字符串s=“{\'project\\”:[{\'name\':\'GCUK\',\'id\':\'project11\',\'href\':\'/httpAuth/app/rest/projects/id:project11\',{\'name\':\'Interiors In Spain\',\'id\':\'project3\',\'href\':“/httpAuth/app/rest/projects/id:project3\'”;
var p=JsonConvert.DeserializeObject;
s=“this”;//便于断点
}
}
[JsonObject(MemberSerialization.OptIn)]
公共类TCProjectWrapper{
[JsonProperty(PropertyName=“project”)]
私有列表项目{get;set;}
}
[JsonObject(MemberSerialization.OptIn)]
公营课程计划{
公共重写字符串ToString(){
返回名称;
}
[JsonProperty(PropertyName=“存档”)]
公共bool存档{get;set;}
[JsonProperty(PropertyName=“description”)]
公共字符串说明{get;set;}
[JsonProperty(PropertyName=“href”)]
公共字符串Href{get;set;}
[JsonProperty(PropertyName=“id”)]
公共字符串Id{get;set;}
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“webUrl”)]
公共字符串WebUrl{get;set;}
}

您接收此消息的方法是什么样子的?整个方法现在都存在了-connection.Request只返回一个JSON字符串-它对itOh没有任何特殊作用,我只是意识到这是一个Silverlight问题。。我从不做Silverlight。。。我不明白为什么我不能在VS中解析这个库…这里没有silverlight-它是纯c#-JsonConvert是Newtonsoft.Json库哦,你应该这么说。。。我在查MSDN JsonConvert的资料那。。。看起来有点不对劲。我认为装饰父对象会更容易。我不认为我可以指望子字符串和每次的长度不幸的是,你还可以创建另一个对象,并使用名为Project的属性反序列化到该对象中,该属性将为你提供一个列表。是的,这就是我最后做的:)这是我曾经做过的我能够下载正确的库。作为参考,我会用我使用的代码更新你的答案。很抱歉,我没有提供完整的示例,我还没有掌握这里的代码示例。