2

I've a JSON like below,

[
  {
    "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 a c# object. 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

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

Why am I getting all UserDocument properties are null, what's wrong here? I'm not getting any error.

Also can you suggest a good example in getting CouchBase queryresult into a .net object.

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
blue
  • 833
  • 2
  • 12
  • 39

2 Answers2

4

Seems your json is not in correct format. If I say your json is like

[
    "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

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);
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • You could also add properties to your `UserDocument` class that return the `createdDate` and `processedDate` properties as `DateTime` values. – krillgar Oct 21 '16 at 16:43
  • @krillgar those are not actual datetime type they are utc double value of equivalent datetiem – Mostafiz Oct 21 '16 at 16:46
  • @Mostafiz Ticks? You can have a property like `public DateTime TaxYear => new DateTime(taxYear);` – krillgar Oct 21 '16 at 17:01
  • yeah good one, thanks :) but I think double is enough for OP's purpose – Mostafiz Oct 21 '16 at 17:03
1

Something like this (using Newtonsoft.Json.Linq):

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

Broodja
  • 11
  • 1