0

I am deserializing an Json string to Object.This is my Json string need to Deserialized.

{
    "response": {
        "total_results": 33,
        "trades": [
            {
                "tid": "E231958349",
                "num": 1,
                "num_iid": 3424234,
                "price": 200.07
            }]
                }
}

I have defined a response model.This is my code:

    public class ResponseModel
    {
        public int total_results { set; get; }
        public List<TradeModel> trades{ set; get;}        
    }

And I also have defined trades model:

    public class TradeModel
    {
        #region Attribute

        #region oid
        private int tid;

        public int Oid
        {
            get { return tid; }
            set { tid = value; }
        }
        #endregion

        #endregion
    }

And this is my deserialize code:

        public static ResponseModel MapResonseJsonToModel(string json)
        {
            ResponseModel orderModel = new ResponseModel();                   
            orderModel = JsonConvert.DeserializeObject<ResponseModel>(json);                   
            return orderModel;
        }

My question is:Why everytime the total_results return 0,and the trades return null?The correct is 33,and not null trade object!

2 Answers 2

2

JsonConvert will be looking for a property named response in ResponseModel due to the format of your JSON. Try changing your JSON to only be the value of response, or create another class like this:

public class ResponseContainer
{
   public ResponseModel response {get;set;} 
}
Sign up to request clarification or add additional context in comments.

Comments

1

The issue here is probably with your JSON format. Your JSON has response as the outermost item and inside of that, it has two sub items namely total_results and trades. For the above code to work, your JSON should be like this :

{
        "total_results": 33,
        "trades": [
            {
                "tid": "E231958349",
                "num": 1,
                "num_iid": 3424234,
                "price": 200.07
            }]       
}

If you intend to keep as it is then change your model to something like Mike said in his answer. You can opt for either of the two ways here.

Hope this clears.

Comments

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.