Problem Deserialising JSON to List<T>
I have a problem Deserialising a JSON string to a List
the TCProject is as follows:
[JsonObject(MemberSerialization.OptIn)]
public class TCProject
{
public override string ToString()
{
return Name;
}
[JsonProperty(PropertyName = "archived")]
public bool Archiv开发者_StackOverflow社区ed { 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; }
}
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: {"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; }
}
I think in this case you need to grab just the array part of the JSON to de-serialize into a List like this:
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;
}
精彩评论