I was able to get your code to work with two changes.
- I changed the
Symbols
property to hold a List<Result>
instead of a List<Symbols>
, since you did not define a Symbols
class that I can see, and the Result
class seems to match the JSON.
- I used an
IsoDateTimeConverter
with a custom date format to handle the TickTime
since the date in the JSON does not conform to the standard ISO 8601 datetime format that Json.Net expects.
Here is the full code:
class Program
{
static void Main(string[] args)
{
string json = @"{""d"": ""{\""TickTime\"":\""29/04/2014 19:13:41\"",\""Symbols\"":[{\""I\"":61,\""H\"":0.8551,\""L\"":0.8516,\""A\"":0.855,\""B\"":0.8545},{\""I\"":62,\""H\"":1301.4,\""L\"":1286.3,\""A\"":1296.6,\""B\"":1296.4}]}""}";
IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
{
DateTimeFormat = "dd/MM/yyyy HH:mm:ss"
};
var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>(json);
var root = JsonConvert.DeserializeObject<RootObject>(outerRoot.d, dateConverter);
Console.WriteLine("TickTime: " + root.TickTime.ToString("dd-MMM-yyyy hh:mm:ss tt"));
foreach (Result r in root.Symbols)
{
Console.WriteLine("I: " + r.I);
Console.WriteLine("A: " + r.A);
Console.WriteLine("B: " + r.B);
Console.WriteLine("H: " + r.H);
Console.WriteLine("L: " + r.L);
Console.WriteLine();
}
}
}
public class OuterRootObject
{
public string d { get; set; }
}
public class RootObject
{
public DateTime TickTime { get; set; }
public List<Result> Symbols { get; set; }
}
public class Result
{
public int I { get; set; }
public double A { get; set; }
public double B { get; set; }
public double H { get; set; }
public double L { get; set; }
}
Output:
TickTime: 29-Apr-2014 07:13:41 PM
I: 61
A: 0.855
B: 0.8545
H: 0.8551
L: 0.8516
I: 62
A: 1296.6
B: 1296.4
H: 1301.4
L: 1286.3