2016-05-24 4 views
1

こんにちはすべて私は、Newtonsoft Jsonを使用してJSONをc#クラスに解析しようとしています。 JSONは私がにシリアライズ解除しようとしているクラスはfollows-の通りです。この次のコードブロック -UWPアプリケーションでJSONを逆シリアル化できない

public static async Task<List<TubeAPI>> GetTubeStatusAsync() 
{ 
string url = 
String.Format("https://api.tfl.gov.uk/Line/central,victoria/Status/detail=true&app_id=&app_key="); 
HttpClient http = new HttpClient(); 
      var response = await http.GetAsync(url); 
      var jsonMessage = await response.Content.ReadAsStringAsync(); 
var result = JsonConvert.DeserializeObject<List<TubeAPI>>(jsonMessage); 
return result; 
} 

経由で取得され

[ 
    { 
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities", 
    "id": "bakerloo", 
    "name": "Bakerloo", 
    "modeName": "tube", 
    "disruptions": [ ], 
    "created": "2016-05-10T15:41:47.997", 
    "modified": "2016-05-10T15:41:47.997", 
    "lineStatuses": [ 
     { 
     "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities", 
     "id": 0, 
     "statusSeverity": 10, 
     "statusSeverityDescription": "Good Service", 
     "created": "0001-01-01T00:00:00", 
     "validityPeriods": [ ] 
     } 
    ], 
    "routeSections": [ ], 
    "serviceTypes": [ 
     { 
     "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities", 
     "name": "Regular", 
     "uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular" 
     } 
    ] 
    }, 
    { 
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities", 
    "id": "central", 
    "name": "Central", 
    "modeName": "tube", 
    "disruptions": [ ], 
    "created": "2016-05-10T15:41:48.45", 
    "modified": "2016-05-10T15:41:48.45", 
    "lineStatuses": [ 
     { 
     "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities", 
     "id": 0, 
     "statusSeverity": 10, 
     "statusSeverityDescription": "Good Service", 
     "created": "0001-01-01T00:00:00", 
     "validityPeriods": [ ] 
     } 
    ], 
    "routeSections": [ ], 
    "serviceTypes": [ 
     { 
     "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities", 
     "name": "Regular", 
     "uri": "/Line/Route?ids=Central&serviceTypes=Regular" 
     } 
    ] 
    } 
] 

follows-ように私が解析しようとしているJSONです

public class TubeAPI 
    { 
     public RootArray[] Property1 { get; set; } 
    } 

    public class RootArray 
    { 
     public string type { get; set; } 
     public string id { get; set; } 
     public string name { get; set; } 
     public string modeName { get; set; } 
     public object[] disruptions { get; set; } 
     public DateTime created { get; set; } 
     public DateTime modified { get; set; } 
     public Linestatus[] lineStatuses { get; set; } 
     public object[] routeSections { get; set; } 
     public Servicetype[] serviceTypes { get; set; } 
    } 

    public class Linestatus 
    { 
     public string type { get; set; } 
     public int id { get; set; } 
     public int statusSeverity { get; set; } 
     public string statusSeverityDescription { get; set; } 
     public DateTime created { get; set; } 
     public object[] validityPeriods { get; set; } 
    } 

    public class Servicetype 
    { 
     public string type { get; set; } 
     public string name { get; set; } 
     public string uri { get; set; } 
    } 

私はデバッグにvar resultでデバッガを設定し、構文解析は、私はクラス内のデータを持っているし、経由するかどうかを確認しました。しかし、クラスには人がいません! enter image description here

ご覧のとおり、Property1フィールドはnullです。私は混乱しています、私はここで間違って何をしていますか?

+0

@mason worked!回答として投稿してください。なぜ今働いているの?私はRootobjectクラスを参照する必要があると考えました(一番上のフィールドのように、私の場合は 'Property1'と思われます) – envyM6

答えて

1

あなたはList<TubeAPI>としています。代わりにList<RootArray>としてデシリアライズする必要があります。これは、JSONに対応する構造体なのでです。あなたはTubeAPIの配列ではなく、RootArrayの配列を持っています。すべてのJSONを引用符で囲むと、TubeAPIという単数形になります。

var result = JsonConvert.DeserializeObject<List<RootArray>>(jsonMessage);