0

how can I deserialize the ff json string:

{"stock":[{"name":"stock1","price":{"currency":"AUD","amount":103.50},"percent_change":-1.33,"volume":1583760,"symbol":"SC1"}],"as_of":"2016-06-10T15:20:00+08:00"}

I've tried the code:

JsonConvert.DeserializeObject<stock>(content);

where content variable is the json string above. However I am getting null value of the properties.

Here are my classes:

public class price
{
    public string currency { get; }

    public double amount { get; }
}


public class stock
{
    public string name { get; }

    public price price { get; }

    public double percent_change { get; }

    public int volume { get; }

    public string symbol { get; }
}

Thank you in advance!

2 Answers 2

2

Add a setter:

public string name { get; set; }

-- update --

You are putting a list of stock into stock.

Add the class:

public class container
{
    public List<stock> Stock { get; set; }
    public string as_of { get; set; }
}

And call:

var result = JsonConvert.DeserializeObject<container>(content);
Sign up to request clarification or add additional context in comments.

1 Comment

hello, I did it to all properties but I still get null values.
0

Use this class for your json string-

public class Price
{
    public string currency { get; set; }
    public double amount { get; set; }
}

public class Stock
{
    public string name { get; set; }
    public Price price { get; set; }
    public double percent_change { get; set; }
    public int volume { get; set; }
    public string symbol { get; set; }
}

public class StockDetails
{
    public List<Stock> stock { get; set; }
    public string as_of { get; set; }
}

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.