问题Deserialising JSON列出< T> [英] Problem Deserialising JSON to List<T>
问题描述
我有Deserialising一个JSON字符串列表的问题。
I have a problem Deserialising a JSON string to a List
TCProject如下:
the TCProject is as follows:
[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; }
}
JSON字符串如下所示:
The JSON string looks as follows:
{"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"}]}
到字符串转换的代码如下:
the code to convert the string is as follows:
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;
}
我得到的异常:
The exception I am getting:
Newtonsoft.Json.JsonSerialisationException:{无法反序列化JSON对象入式'System.Collections.Generic.List`1 [TCProject]'}
Newtonsoft.Json.JsonSerialisationException: {"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[TCProject]'."}
有一定是东西很容易,我很想念? - 任何人有任何想法
there has got to be something really easy that i'm missing - anyone got any ideas?
推荐答案
我敢肯定,如果你创建了一个属性叫做项目的一类,这是一个列表和反序列化到该对象,这一切都只是工作。
I am pretty sure if you created a class with one property called project and that was a List and deserialized to that object, that everything would just work.
//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; }
}
这篇关于问题Deserialising JSON列出< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!