2016-03-23 14 views
1

bu tür serileştirmek için nasıl yardım bekliyoruz, ben bir başlangıç. Sorunu çözmek için yönetmeksizin, bu problem gibi bir şeye benzeyen bir şey arıyordum. Ve şimdi bir süredir bu problemle uğraştım. Bir json bağlantısında teslimat zamanı, adres vb. Gibi bilgileri alabileceğimiz yerel posta teslimatımızdan bir API kullanıyorum. Ama bu durumda json biraz farklı görünüyor ve onunla nasıl çalışacağımı bilmiyorum.C# Başlamak için json

Birisi bana teslim ve adres gibi verilere nasıl erişebileceğimi açıklayabilirse gerçekten mutlu olurdum. Şimdi denediğimde, sadece değerin sıfır olduğunu gösteriyor. bunu nasıl göründüğünü görebilirsiniz

Ben

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

Tekrar merhaba beyler. Birisi bana kamu sınıfını anlatabilir mi? ( public Trackinginformationresponse TrackingInformationResponse {get; set; } } genel sınıf İzlemeYönetme { herkese açık Gönderi [] gönderiler {get; set; } –

+0

Lütfen, satın aldığınız JSON dizesini gönderin, böylece serileştirmenin girdisinin ne olduğunu görebiliriz. –

+0

Merhaba, bilgi böyle görünüyor. Bundan kurtulmak istediğim, alıcı ve tahminiTimeOfArrival'dan gelen bilgiler. –

cevap

0

, JsonConvert.DeserializeObject<>() ile deneyebilirsiniz ihtiyacınız yöntemdir. Bundan sonra, sadece seri hale getirilmiş veri yapısını geçmek zorundasınız.

Niçin seri hale getirilmeye çalışıldıktan sonra NULL aldığınıza dair olarak, deserializtion başarısız olursa NULL döndürülür. Genellikle, veriler kötü bir biçimde olduğunda veya karakter durumu eşleşmediğinde. Ancak, sağladığınız örnek JSON için, sınıflarınızı kullanarak benim için doğru şekilde sorun çözülmedi.

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

Çok teşekkür ederim. Rootobject root = JsonConvert.DeserializeObject (veri) 'ye sahip olduğumu anlamadım; Sevkiyat irsaliyesi = root.TrackingInformationResponse.shipments [0]; Ama şimdi verileri göstermek gibi görünüyor! –

+0

Merhaba, [] öğesini sınıftaki gönderiden kullanmak istediğimde nasıl yapacağımı açıklayabilmenin herhangi bir yolu var mı? Bir dizi olduğu için onu bir metin kutusunda gösteremiyorum. –

0

diğer cevaplar devlet gibi

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