2015-07-28 125 views
0

我有一個json字符串,它工作正常。現在,當我添加嵌套項目時,它不解析。我想解析在C#中的JSON數組。這是我的json代碼。JsonConvert無法解析

{ 
    "Type": "Hotel",  
    "myArray": [{ 
     "id": 0, 
     "time": ["1", "2"], 
     "index": 0, 
     "picked": [{ 
      "id": 1, 
      "oc": "1" 
     }, { 
      "id": 2, 
      "oc": "1" 
     }] 
    }, { 
     "id": 1, 
     "time": [], 
     "index": 1, 
     "picked": [] 
    }, { 
     "id": 2, 
     "time": [], 
     "index": 2, 
     "picked": [] 
    }, { 
     "id": 3, 
     "time": [], 
     "index": 3, 
     "picked": [] 
    }, { 
     "id": 4, 
     "time": [], 
     "index": 4, 
     "picked": [] 
    }, { 
     "id": 5, 
     "time": [], 
     "index": 5, 
     "picked": [] 
    }, { 
     "id": 6, 
     "time": ["3"], 
     "index": 6, 
     "picked": [{ 
      "id": 3, 
      "oc": "1" 
     }] 
    }] 
} 

我想這樣

JsonConvert.DeserializeObject<MyObject>(abovejsonstring) 

任何人幫助我。

當前階層結構是

public class MyObject 
    { 
     public string Type { get; set; } 
     public List<MyArray> myArray { get; set; } 
    } 

    public class MyArray 
    { 
     public string id { get; set; } 
     public string[] time { get; set; } 
     public string index { get; set; } 
     public List<Picked> picked { get; set; } 
    } 
    public class Picked 
    { 
     public string id { get; set; } 
     public string oc { get; set; } 
    } 

錯誤:

無法反序列化當前JSON對象(例如{ 「名稱」: 「值」})到類型「System.String []',因爲該類型需要JSON數組(例如[1,2,3])才能正確地反序列化。要解決這個錯誤,可以將JSON更改爲JSON數組(例如[1,2,3])或更改反序列化類型,以便它是一個正常的.NET類型(例如,不是像整數這樣的基本類型,也不是類似的集合類型一個數組或列表),可以從JSON對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從JSON對象反序列化。

+1

什麼是錯誤? –

+0

你能告訴我們什麼是錯誤? – Ajit

+0

更新了我的信息 –

回答

0

我試過

http://json2csharp.com/

http://jsonclassgenerator.codeplex.com/

精湛。加工。

public class WeekArray2 
    { 

     [JsonProperty("id")] 
     public int id { get; set; } 

     [JsonProperty("time")] 
     public string[] time { get; set; } 

     [JsonProperty("index")] 
     public int index { get; set; } 

     [JsonProperty("picked")] 
     public Picked2[] picked { get; set; } 
    } 


    public class MS 
    { 

     [JsonProperty("year")] 
     public string year { get; set; } 

     [JsonProperty("month")] 
     public string month { get; set; } 

     [JsonProperty("currentmonth")] 
     public string currentmonth { get; set; } 

     [JsonProperty("community")] 
     public string community { get; set; } 

     [JsonProperty("WeekArray")] 
     public WeekArray2[] weekarray { get; set; } 
    } 
0

我認爲問題是,這是結構牛頓從JSON看到:

public class MyArray 
{ 
    public int id { get; set; } 
    public List<object> time { get; set; } 
    public int index { get; set; } 
    public List<object> picked { get; set; } 
} 

所以,你應該改變數組(字符串[]時間)時間列表

編輯:剛剛看到它不是時間陣列,那可能是因爲它不識別「Picked」對象

+0

Earler它工作正常,但我又添加了一個對象myArray,然後失敗了。 –