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.

4
  • processedDate and createdDate are not strings. Commented Oct 21, 2016 at 16:21
  • possible duplicate stackoverflow.com/questions/4611031/… Commented Oct 21, 2016 at 16:22
  • your json is wrong Commented Oct 21, 2016 at 16:23
  • @RhinoDevel - I've corrected the type problem, still I've the same null issue Commented Oct 21, 2016 at 16:28

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

You could also add properties to your UserDocument class that return the createdDate and processedDate properties as DateTime values.
@krillgar those are not actual datetime type they are utc double value of equivalent datetiem
@Mostafiz Ticks? You can have a property like public DateTime TaxYear => new DateTime(taxYear);
yeah good one, thanks :) but I think double is enough for OP's purpose
1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.