我之前有一根json字符串,很好用。现在,当我添加嵌套项时,它不会进行解析。我想在c#中解析json数组。这是我的json密码。
{
"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),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。
发布于 2015-07-28 11:17:55
我试过
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; }
}发布于 2015-07-28 10:32:02
我认为问题是,这是牛顿从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; }
}因此,应该将数组(string[]时间)更改为列出时间。
编辑:刚看到的不是时间数组,那可能是因为它不识别“选中的”对象
https://stackoverflow.com/questions/31673156
复制相似问题