문제

i have problem in deserialize JSON using C# in windows phone 8 please help me to do it:-

This the JSON result i have:-

{"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}]}"}

This is class i use:-

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;
} 

i have used this code to deserlized this JSON but its not working because inside outer root result is Time Value and Symbols array and i have no idea how to parse them at the same time.

var outerRoot = JsonConvert.DeserializeObject<OuterRootObject>(My JSON);
var root = JsonConvert.DeserializeObject<RootObject>(outerRoot.d);
도움이 되었습니까?

해결책

This working 100% just change the

public DateTime TickTime { get; set; }

to

public string TickTime { get; set; }

Then use this to Deserialize your 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;
                             }

다른 팁

I was able to get your code to work with two changes.

  1. 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.
  2. 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top