1

我現在正在做一個windows phone項目,並且需要請求一些json數據的web服務。 如果json結構像 [Dictionary1, Dictionary2, Dictionary3 ] 然後,DataContractJsonSerializer工作正常。 但接下來的請求我會得到一個複雜的json數據,它不起作用。像 [ [Dictionary1], [Dictionary2], [Dictionary3] ]Windows Phone,如何反序列化嵌套的json Array

真實數據是:

[ 

     [{"length":5734.042,"duration":1680,"legs": 
      [ 
       {"length":685,"duration":555.42,"type":"walk","locs": 

        [ 

         {"coord":{"x":2546445,"y":6675512},"arrTime":"201203290959","depTime":"201203290959","name":null}, 
         {"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203291000","depTime":"201203291000","name":"Teekkaripolku"} 
        ] 
       }, 


       {"length":4158,"duration":420,"type":"5","code":"2506 2","locs": 
        [ 
         {"coord":{"x":2546168,"y":6674959},"arrTime":"201203291009","depTime":"201203291009","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"}, 
         {"coord":{"x":2546337,"y":6674857},"arrTime":"201203291009","depTime":"201203291009","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"} 
        ] 
       } 
      ] 
     }], 


     [{"length":5734.042,"duration":1680,"legs": 
      [ 
       {"length":685,"duration":555.42,"type":"1", "code":"1111", "locs": 
        [ 
         {"coord":{"x":2546445,"y":6675512},"arrTime":"201203290952","depTime":"201203290952","name":null}, 
         {"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203290953","depTime":"201203290953","name":"Teekkaripolku"} 
        ] 
       }, 
       {"length":4158,"duration":420,"type":"5","code":"2194 2","locs": 
        [ {"coord":{"x":2546168,"y":6674959},"arrTime":"201203291002","depTime":"201203291002","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"}, 
         {"coord":{"x":2546337,"y":6674857},"arrTime":"201203291002","depTime":"201203291002","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"} 
        ] 
       } 
      ] 
     }] 

    ] 

和類模型是:

[DataContract] 
public class RouteList 
{ 
    [DataMember] 
    public List<Route> oneRoute; 

} 

--- 
    [DataContract] 
public class Route 
{ 
    [DataMember(Name = "length", IsRequired = true)] 
    public Double length { get; set; } 

    [DataMember(Name = "duration", IsRequired = true)] 
    public Double duration { get; set; } 

    [DataMember(Name = "legs", IsRequired = true)] 
    public List<Leg> legs { get; set; } 
} 

---- 
    [DataContract] 
public class Leg 
{ 
    [DataMember(Name = "length", IsRequired = true)] 
    public Double length { get; set; } 

    [DataMember(Name = "duration", IsRequired = true)] 
    public Double duration { get; set; } 

    [DataMember(Name = "type", IsRequired = true)] 
    public String type { get; set; } 

    [DataMember(Name = "code", IsRequired = false)] 
    public String code { get; set; } 

    [DataMember(Name = "locs", IsRequired = true)] 
    public List<Loc> locs { get; set; } 

    [DataMember(Name = "shape", IsRequired = false)] 
    public String shape { get; set; } 
} 

----- 

    [DataContract] 
public class Loc 
{ 
    [DataMember(Name = "coord", IsRequired = true)] 
    public String coord { get; set; } 

    [DataMember(Name = "arrTime", IsRequired = true)] 
    public String arrTime { get; set; } 

    [DataMember(Name = "depTime", IsRequired = true)] 
    public String depTime { get; set; } 

    [DataMember(Name = "name", IsRequired = true)] 
    public String name { get; set; } 

    [DataMember(Name = "code", IsRequired = false)] 
    public String code { get; set; } 

    [DataMember(Name = "shortCode", IsRequired = false)] 
    public String shortCode { get; set; } 

    [DataMember(Name = "stopAddress", IsRequired = false)] 
    public String stopAddress { get; set; } 

} 

和反序列化代碼:

System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(e.Result)); 
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<RouteList>)); 
result = (List<RouteList>)serializer.ReadObject(mStream); 

我已經嘗試過DataContractJsonSerializer和json.NET。但失敗了。最可能的問題是嵌套數組,模型:RouteList。任何人都知道如何解決它?非常感謝。

+2

爲了得到一些幫助,請添加代碼的樣本.. – 2012-03-30 06:11:08

+0

@ Alaa.Ali heyAlaa,我已經更新,添加的類模型和反序列化的代碼。非常感謝 – NOUX 2012-03-31 02:44:06

回答

1

這個問題可能是因爲你的類的創建,在結束之前檢查你處理解析結果的模型類。或者,如果你想幫助添加更多的信息,比如你的模型類的結構,你用於解析json等的代碼。這裏似乎沒有任何東西被壓縮。 Json.Net將做所有的技巧,你需要採取的唯一的努力是適當的建設你的類型.. 享受編碼

+0

嘿Rakesh,我已經更新了內容,添加了班級模型,你會看看嗎?我搜索了互聯網,並沒有發現任何帖子如何反序列化嵌套數組,如[[array1],[array2]],任何想法,非常感謝。 – NOUX 2012-03-31 02:36:58

1

在奧利薩里吉維的幫助下,問題得到解決。 在他的許可下,在這裏發佈他的解決方案,以防其他人可能需要它。

// The request response handler 

var routeResults = new List<CompoundRoute>(); 

if (e.Result.Trim().Length != 0) 
{ 
    JArray json = JArray.Parse(e.Result); 

    foreach (var token in json) 
{ 
    var routeArray = token as JArray; 
    if (routeArray != null) 
    { 
     var compoundRoute = new CompoundRoute { Routes = new Route[routeArray.Count] }; 
     for (int i = 0; i < compoundRoute.Routes.Length; ++i) 
     { 
      compoundRoute.Routes[i] = new Route(routeArray[i]); 
     } 
     routeResults.Add(compoundRoute); 
    } 
} 
} 

// The data model 

[DataContract] 
public class CompoundRoute 
{ 
    private static readonly Route[] EmptyRoutes = new Route[0]; 

    public CompoundRoute() 
    { 
     Routes = EmptyRoutes; 
    } 

    [DataMember] 
    public Route[] Routes; 
} 

[DataContract] 
public class Route 
{ 
    public Route(JToken token) 
    { 
    Length = token.Value<double>("length"); 

    int durationSeconds = token.Value<int>("duration"); 
    Duration = TimeSpan.FromSeconds(durationSeconds); 

    JArray legTokens = token["legs"] as JArray; 
    if (legTokens != null) 
    { 
     Legs = new Leg[legTokens.Count]; 
     for (int i = 0; i < Legs.Length; ++i) 
     { 
      Legs[i] = new Leg(legTokens[i]); 
     } 
    } 
    } 

    [DataMember] 
    public double Length; 
    [DataMember] 
    public TimeSpan Duration; 
    [DataMember] 
    public Leg[] Legs; 
} 

[DataContract] 
public class Leg 
{ 
    public Leg(JToken token) 
    { 
    Length = token.Value<double>("length"); 

    double durationSeconds = token.Value<double>("duration"); 
    Duration = TimeSpan.FromSeconds((int)Math.Round(durationSeconds)); 

    Type = token.Value<string>("type"); 

    string lineCode = token.Value<string>("code"); 
    if (lineCode != null) 
    { 
     Line = App.Cache.GetOrCreate(lineCode,() => new Line(lineCode)); 
    } 

    if (Type == "12") // Commuter trains 
    { 
     Line.ShortName = Utils.RemoveLeadingNumbers(Line.ShortName); 
    } 

    JArray locTokens = token["locs"] as JArray; 
    if (locTokens != null) 
    { 
     Locs = new LegLocation[locTokens.Count]; 
     for (int i = 0; i < Locs.Length; ++i) 
     { 
      Locs[i] = new LegLocation(locTokens[i]); 
     } 
    } 
    else 
    { 
     Locs = new LegLocation[0]; 
    } 

    JArray shapeTokens = token["shape"] as JArray; 
    if (shapeTokens != null) 
    { 
     Shape = new ReittiCoordinate[shapeTokens.Count]; 
     for (int i = 0; i < Shape.Length; ++i) 
     { 
      var shapeToken = shapeTokens[i]; 
      double x = shapeToken.Value<double>("x"); 
      double y = shapeToken.Value<double>("y"); 
      var coordinate = new ReittiCoordinate(y, x); 
      Shape[i] = coordinate; 
     } 
    } 
} 

[DataMember] 
public double Length; 
[DataMember] 
public TimeSpan Duration; 
[DataMember] 
public string Type; 
[DataMember] 
public Line Line; 
[DataMember] 
public LegLocation[] Locs; 
[DataMember] 
public ReittiCoordinate[] Shape; 
} 

[DataContract] 
public class LegLocation 
{ 
    public LegLocation(JToken token) 
    { 
    var coordToken = token["coord"]; 
    double x = coordToken.Value<double>("x"); 
    double y = coordToken.Value<double>("y"); 
    Coord = new ReittiCoordinate(y, x); 

    string arrTimeString = token.Value<string>("arrTime"); 
    ArrTime = DateTime.ParseExact(arrTimeString, "yyyyMMddHHmm", null); 

    string depTimeString = token.Value<string>("depTime"); 
    DepTime = DateTime.ParseExact(depTimeString, "yyyyMMddHHmm", null); 

    Name = token.Value<string>("name"); 
} 

[DataMember] 
public ReittiCoordinate Coord; 
[DataMember] 
public DateTime ArrTime; 
[DataMember] 
public DateTime DepTime; 
[DataMember] 
public string Name; 
} 
+0

最近很抱歉,這似乎是一個很好的解決方案。 – 2012-04-04 05:55:29