我在Windows Phone 8中使用C#反序列化JSON时遇到问题请帮助我这样做: -
这是我的JSON结果: -
{" d":" {\" TickTime \":\" 29/04/2014 19:13:41 \" ,\"符号\":[{\" I \":61,\" H \":0.8551,\&#34,L \ ":0.8516,\" A \":0.855,\"乙\":0.8545},{\" I \" 62 ,\" H \":1301.4,\&#34,L \":1286.3,\" A \":1296.6,\"乙\ ":1296.4}]}"}
这是我使用的课程: -
public class OuterRootObject
{
public string d { get; set; }
}
public class RootObject
{
public DateTime TickTime { get; set; }
public List<Symbols> 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;
}
我已经使用这段代码来保存这个JSON,但它没有用,因为内部根结果是Time Value和Symbols数组,我不知道如何同时解析它们。
var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>(My JSON);
var root = JsonConvert.DeserializeObject<RootObject>(outerRoot.d);
答案 0 :(得分:0)
我能够让您的代码进行两项更改。
Symbols
属性以保留List<Result>
而不是List<Symbols>
,因为您没有定义我可以看到的Symbols
类,以及{{ 1}}类似乎与JSON匹配。Result
来处理IsoDateTimeConverter
,因为JSON中的日期不符合Json.Net期望的标准ISO 8601日期时间格式。以下是完整代码:
TickTime
输出:
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; }
}
答案 1 :(得分:0)
这100%工作只是改变了
public DateTime TickTime { get; set; }
到
public string TickTime { get; set; }
然后使用它来反序列化您的JSON
var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>(your JSON);
var root = JsonConvert.DeserializeObject<RootObject>(outerRoot.d);
foreach (var SymbolsInfo in root.Symbols)
{
var i = SymbolsInfo .I;
var a = SymbolsInfo .A;
var b = SymbolsInfo .B;
var l = SymbolsInfo .L;
var h = SymbolsInfo .H;
}