简体   繁体   English

将JSON数组转换为ac#对象集合

[英]Convert JSON array to a c# object collection

I've a JSON like below, 我有一个如下所示的JSON,

[
  {
    "document":
            {
                "createdDate":1476996267864,
                "processedDate":1476996267864,
                "taxYear":"2015",
                "type":"user_document"
            }
    },
  {
     "document":
            {
                "createdDate":1476998303463,
                "processedDate":0,
                "taxYear":"2015",
                "type":"user_document"
            }
    }
  ]

I need to convert it into ac# object. 我需要将其转换为ac#对象。 My object type is as below- 我的对象类型如下-

public class UserDocument
    {
        [JsonProperty(PropertyName = "type")]
        public string type { get; set; }

        [JsonProperty(PropertyName = "taxYear")]
        public string taxYear { get; set; }

        [JsonProperty(PropertyName = "createdDate")]
        public string createdDate { get; set; }

        [JsonProperty(PropertyName = "processedDate")]
        public string processedDate { get; set; }

    }

I'm using below code to deserialize the json but all UserDocument properties are null 我正在使用下面的代码反序列化json,但是所有UserDocument属性均为null

 var test = JsonConvert.DeserializeObject<List<UserDocument>>(jsonString);

Why am I getting all UserDocument properties are null, what's wrong here? 为什么我所有的UserDocument属性都为null,这怎么了? I'm not getting any error. 我没有任何错误。

Also can you suggest a good example in getting CouchBase queryresult into a .net object. 您还可以建议一个将CouchBase查询结果转换为.net对象的好例子。

Seems your json is not in correct format. 似乎您的json格式不正确。 If I say your json is like 如果我说你的json就像

[
    "document":
            {
                "createdDate":1476996267864,
                "processedDate":1476996267864,
                "taxYear":"2015",
                "type":"user_document"
            },

     "document":
            {
                "createdDate":1476998303463,
                "processedDate":0,
                "taxYear":"2015",
                "type":"user_document"
            }
  ]

Then create a model like 然后创建一个像

public class Document
{
   public UserDocument document {get;set;}
}

and change your UserDocument model's createdDate and processedDate properties as double because its like that in your json 改变你的UserDocument模型的createdDateprocessedDate性能double ,因为它就像在你的JSON

public class UserDocument
    {
        [JsonProperty(PropertyName = "type")]
        public string type { get; set; }

        [JsonProperty(PropertyName = "taxYear")]
        public string taxYear { get; set; }

        [JsonProperty(PropertyName = "createdDate")]
        public double createdDate { get; set; }

        [JsonProperty(PropertyName = "processedDate")]
        public double processedDate { get; set; }

    }

and then deserialize 然后反序列化

var test = JsonConvert.DeserializeObject<List<Document>>(jsonString);

这样的事情(使用Newtonsoft.Json.Linq):

var documents = JArray.Parse(json).Select(t => t["document"].ToObject<UserDocument>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM