JsonConvert无法解析



我有一个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)

谁来帮我一下。

当前Class结构为

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; }
    }

Error was:

无法将当前JSON对象(例如{"name":"value"})反序列化为类型'System '。String[]',因为该类型需要一个JSON数组(例如[1,2,3])来正确反序列化。要修复此错误,要么将JSON更改为JSON数组(例如[1,2,3]),要么更改反序列化类型,使其成为可以从JSON对象反序列化的普通。net类型(例如,不是像整数这样的原始类型,也不是像数组或列表这样的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象进行反序列化。

我认为问题在于这是newton从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[] time)为List time

编辑:刚才看到它不是时间数组,那么可能是因为它不识别" selected "对象

I Tried with

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; }
    }

相关内容

  • 没有找到相关文章

最新更新