0

I am using JSON.NET to deserialize but having the object instantiated with all its properties set to null.

JSON

{
    "verify-purchase":
    {
        "item_name":"Pipeline.NET Task Scheduler",
        "item_id":"1111111",
        "created_at":"Wed Jun 12 15:56:02 +1000 2013",
        "buyer":"xxxxxxxx",
        "licence":"Regular License"
    }
}

C# Class

public class VerifyPurchase
{
     [JsonProperty("item_name")]
     public string ItemName { get; set; }

     [JsonProperty("item_id")]
     public string ItemId { get; set; }

     [JsonProperty("created_at")]
     public string CreatedAt { get; set; }

     [JsonProperty("buyer")]
     public string Buyer { get; set; }

     [JsonProperty("licence")]
     public string Licence { get; set; }
}

C# Deserialization

var purchase = JsonConvert.DeserializeObject<VerifyPurchase>(jsonText);

This seems simple enough. What is going wrong here to result in NULL properties?

1 Answer 1

1

You could have a wrapper class allowing you to specify the verify-purchase property:

public class Wrapper
{
    [JsonProperty("verify-purchase")]
    public VerifyPurchase Purchase { get; set; }
}

that you would deserialize:

var wrapper = JsonConvert.DeserializeObject<Wrapper>(jsonText);
VerifyPurchase purchase = wrapper.Purchase;
// the purchase.* properties should be assigned at this stage
Sign up to request clarification or add additional context in comments.

1 Comment

I tried the wrapper before but didn't include the attribute. Thanks for the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.