2015-07-28 156 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,然后失败了。 –