2016-03-23 2 views
1

を非直列化する方法については、私は初心者です。私は問題を解決することなくこの問題のようなものを探しています。そして今私はしばらくこの問題に立ち往生しています。私は、私たちが配送時間、住所などの情報をjsonリンクで得ることができる私たちの地方の配達のAPIを使用しています。しかし、この場合、jsonは少し違って見えますが、私はそれをどのように処理するのか分かりません。C#この種のjson

配送先や住所などのデータにどのようにアクセスできるのか誰かが私に説明できると本当にうれしいです。今私が試してみると、値がnullであることだけが示されます。私はあなたがそれが...

private async void button_Click(object sender, RoutedEventArgs e) 
{ 
    string url = "https://api2.postnord.com/rest/shipment/v1/trackandtrace/findByIdentifier.json?id=..."; 

    HttpClient client = new HttpClient(); 
    string date = await client.GetStringAsync(new Uri(url)); 
    var jarray = JsonConvert.DeserializeObject<Rootobject>(date); 
} 

public class Rootobject 
{ 
    public Trackinginformationresponse TrackingInformationResponse { get; set; } 
} 

public class Trackinginformationresponse 
{ 
    public Shipment[] shipments { get; set; } 
} 

public class Shipment 
{ 
    public string shipmentId { get; set; } 
    public string uri { get; set; } 
    public int assessedNumberOfItems { get; set; } 
    public DateTime deliveryDate { get; set; } 
    public DateTime estimatedTimeOfArrival { get; set; } 
    public Service service { get; set; } 
    public Consignor consignor { get; set; } 
    public Consignee consignee { get; set; } 
    public Statustext statusText { get; set; } 
    public string status { get; set; } 
    public Totalweight totalWeight { get; set; } 
    public Totalvolume totalVolume { get; set; } 
    public Assessedweight assessedWeight { get; set; } 
    public Item[] items { get; set; } 
    public Additionalservice[] additionalServices { get; set; } 
    public object[] splitStatuses { get; set; } 
    public Shipmentreference[] shipmentReferences { get; set; } 
} 

public class Service 
{ 
    public string code { get; set; } 
    public string name { get; set; } 
} 

public class Consignor 
{ 
    public string name { get; set; } 
    public Address address { get; set; } 
} 

public class Address 
{ 
    public string street1 { get; set; } 
    public string city { get; set; } 
    public string countryCode { get; set; } 
    public string country { get; set; } 
    public string postCode { get; set; } 
} 

public class Consignee 
{ 
    public Address1 address { get; set; } 
} 

public class Address1 
{ 
    public string city { get; set; } 
    public string countryCode { get; set; } 
    public string country { get; set; } 
    public string postCode { get; set; } 
} 

public class Statustext 
{ 
    public string header { get; set; } 
    public string body { get; set; } 
} 

public class Totalweight 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Totalvolume 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Assessedweight 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Item 
{ 
    public string itemId { get; set; } 
    public DateTime dropOffDate { get; set; } 
    public DateTime deliveryDate { get; set; } 
    public string typeOfItemActual { get; set; } 
    public string typeOfItemActualName { get; set; } 
    public string status { get; set; } 
    public Statustext1 statusText { get; set; } 
    public Statedmeasurement statedMeasurement { get; set; } 
    public Assessedmeasurement assessedMeasurement { get; set; } 
    public Event[] events { get; set; } 
    public Reference[] references { get; set; } 
    public object[] itemRefIds { get; set; } 
    public object[] freeTexts { get; set; } 
} 

public class Statustext1 
{ 
    public string header { get; set; } 
    public string body { get; set; } 
} 

public class Statedmeasurement 
{ 
    public Weight weight { get; set; } 
    public Length length { get; set; } 
    public Height height { get; set; } 
    public Width width { get; set; } 
    public Volume volume { get; set; } 
} 

public class Weight 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Length 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Height 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Width 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Volume 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Assessedmeasurement 
{ 
    public Weight1 weight { get; set; } 
} 

public class Weight1 
{ 
    public string value { get; set; } 
    public string unit { get; set; } 
} 

public class Event 
{ 
    public DateTime eventTime { get; set; } 
    public string eventCode { get; set; } 
    public string status { get; set; } 
    public string eventDescription { get; set; } 
    public Location location { get; set; } 
} 

public class Location 
{ 
    public string displayName { get; set; } 
    public string name { get; set; } 
    public string locationId { get; set; } 
    public string countryCode { get; set; } 
    public string country { get; set; } 
    public string postcode { get; set; } 
    public string city { get; set; } 
    public string locationType { get; set; } 
} 

public class Reference 
{ 
    public string value { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
} 

public class Additionalservice 
{ 
    public string code { get; set; } 
    public string name { get; set; } 
} 

public class Shipmentreference 
{ 
    public string value { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
} 
{ 
    "TrackingInformationResponse": { 
     "shipments": [{ 
      "shipmentId": "85319760154SE", 
      "uri": "/ntt-service-rest/api/shipment/85319760154SE/0", 
      "assessedNumberOfItems": 1, 
      "deliveryDate": "2016-03-22T20:35:00", 
      "estimatedTimeOfArrival": "2016-03-22T13:41:00", 
      "service": { 
       "code": "19", 
       "name": "MyPack" 
      }, 
      "consignor": { 
       "name": "H&M", 
       "address": { 
        "street1": "HULTAGATAN 47", 
        "city": "BORÅS", 
        "countryCode": "SWE", 
        "country": "Sweden", 
        "postCode": "50189" 
       } 
      }, 
      "consignee": { 
       "address": { 
        "city": "UDDEVALLA", 
        "countryCode": "SWE", 
        "country": "Sweden", 
        "postCode": "45133" 
       } 
      } 
     }] 
    } 
} 
+0

こんにちは。誰かが私に理解させることができますか? パブリッククラスRootobject { public Trackinginformationresponse TrackingInformationResponse {get;セット; } } public class Trackinginformationresponse { 公開出荷[]出荷{get;セット; } –

+0

JSON文字列を投稿して、非直列化の入力を確認してください。 –

+0

こんにちは、これは情報のように見えます。私がこれから出たいのは、荷受人からの情報とestimatedTimeOfArrivalです。 –

答えて

0

で試すことができます。その後、デシリアライズされたデータ構造をトラバースする必要があります。

逆シリアル化を試みた後にNULLを取得する理由は、逆シリアル化が失敗した場合はNULLが返されます。通常、データが不正な形式になっているか、大文字と小文字が一致しない場合。しかし、あなたが提供したサンプルJSONのために、私はあなたのクラスを使って正しく消滅しました。

Rootobject root = JsonConvert.DeserializeObject<Rootobject>(data); 
if (root == null || 
    root.TrackingInformationResponse == null || 
    root.TrackingInformationResponse.shipments == null || 
    root.TrackingInformationResponse.shipments.Length == 0) 
{ 
    throw new Exception(); 
} 
Shipment shipment = root.TrackingInformationResponse.shipments[0]; 

if (shipment.consignee == null || shipment.consignee.address == null) 
{ 
    throw new Exception(); 
} 

Console.WriteLine("Consignee: {0}", shipment.consignee); 
Console.WriteLine("Estimated Time of Arrival: {0}", shipment.estimatedTimeOfArrival); 
+0

ありがとう、ありがとう。私は理解していなかった ルートオブジェクトルート= JsonConvert.DeserializeObject (データ); 出荷出荷= root.TrackingInformationResponse.shipments [0]; 今、データを表示するように見えます! –

+0

こんにちは、クラスの荷物からアイテム[]を使用したいと思ったらどうすればいいのか説明できますか?それは配列なので、テキストボックスに表示することはできません。 –

2

どのように見えるかを見ることができます下

をデシリアライズするためにjson.netを使用

ここであなたを助けるかもしれない既存のポストです。

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

ここNewtonsoft上のいくつかの情報です。使い方のhttp://www.newtonsoft.com/json/help/html/deserializeobject.htm

例:

Newtonsoft.Json.JsonConvert.DeserializeObject() 
+0

うん、Newtonsoft.JSONは行く道です –

0

あなたはJsonConvert.DeserializeObject<>()はあなたが必要とする方法であり、他の回答の状態と同様

var jarray = JsonConvert.DeserializeObject<Rootobject>(date, 
    new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });