2011-07-11 18 views
4

リストへ問題Deserialising JSONは私がリストにJSON文字列をDeserialising問題を抱えている<T>

TCProjectは、次のとおりです。次のように

[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文字列を検索します:

{"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; 
} 
私は取得しています

例外:

Newtonsoft.Json.JsonSerialisationException:{ "型にJSONオブジェクトをデシリアライズすることはできません 'System.Collections.Generic.List`1 [TCProject]'"}

私は行方不明の何かが本当に簡単になってしまっています。

+0

これを受け取った方法はどのように見えますか? – jcolebrand

+0

メソッド全体が現在存在しています - _connection.RequestはJSON文字列を返すだけです - それは特別なことをしません – stack72

+0

ああ、これは一般的にSilverlightの問題であることを認識しただけです。なぜ私はVSでそのライブラリを解決することができなかったのかを理解してください... – jcolebrand

答えて

3

私はあなたが呼ばつのプロパティを持つクラスを作成した場合、かなり確信していますそれはリストであり、そのオブジェクトに逆シリアル化され、すべてがうまくいくだろうということでした。

//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; } 
} 
+0

あなたは先生は天才です!本当にありがとう! – stack72

+0

正しいライブラリをダウンロードできたら、これは私がやったことです。参考までに、使用したコードで回答を更新します。 – jcolebrand

+0

申し訳ありませんが、私は完全なサンプルを提供していませんでした。 –

0

私はこのケースでは、あなたがこのようなリストにデシリアライズするために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; 
    } 
+0

それは少しオフのようだ。私は親オブジェクトを装飾する方が簡単だと思います。 – jcolebrand

+0

残念なことに、サブストリングとその長さをカウントできるとは思わない – stack72

+0

次に、Projectというプロパティを持つ別のオブジェクトを作成し、そのオブジェクトに逆シリアル化すると、そのプロパティにリストが表示されます。 – JTWebMan

関連する問題