首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析的JsonConvert

无法解析的JsonConvert
EN

Stack Overflow用户
提问于 2015-07-28 10:04:52
回答 2查看 199关注 0票数 0

我之前有一根json字符串,很好用。现在,当我添加嵌套项时,它不会进行解析。我想在c#中解析json数组。这是我的json密码。

代码语言:javascript
复制
{
    "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"
        }]
    }]
}

我想要这样

代码语言:javascript
复制
JsonConvert.DeserializeObject<MyObject>(abovejsonstring)

有人来帮我。

当前的类结构是

代码语言:javascript
复制
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对象反序列化。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-28 11:17:55

我试过

http://json2csharp.com/

http://jsonclassgenerator.codeplex.com/

太棒了。工作中。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-07-28 10:32:02

我认为问题是,这是牛顿从json中看到的结构:

代码语言:javascript
复制
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[]时间)更改为列出时间。

编辑:刚看到的不是时间数组,那可能是因为它不识别“选中的”对象

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31673156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档