C#正在寻找如何反序列化此类json的帮助

C#正在寻找如何反序列化此类json的帮助,c#,arrays,json,win-universal-app,C#,Arrays,Json,Win Universal App,首先,我是个初学者。我一直在寻找类似于这个问题的东西,但没有设法解决这个问题。现在我已经被这个问题困扰了一段时间。我使用的API来自我们本地的post delivery,在这里我们可以通过json链接获取信息,如deliverytime、address等。但在本例中,json看起来有点不同,我不知道如何使用它 如果有人能向我解释我如何访问数据,如deliverdate和address,我会非常高兴。现在当我尝试时,它只显示值为null 我使用json.net进行反序列化 下面你可以看到它的样子

首先,我是个初学者。我一直在寻找类似于这个问题的东西,但没有设法解决这个问题。现在我已经被这个问题困扰了一段时间。我使用的API来自我们本地的post delivery,在这里我们可以通过json链接获取信息,如deliverytime、address等。但在本例中,json看起来有点不同,我不知道如何使用它

如果有人能向我解释我如何访问数据,如deliverdate和address,我会非常高兴。现在当我尝试时,它只显示值为null

我使用json.net进行反序列化

下面你可以看到它的样子

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

这里有一个现有的帖子可能会对你有所帮助

这里有一些关于Newtonsoft的信息

用法示例:

Newtonsoft.Json.JsonConvert.DeserializeObject()
你可以试试看

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

与其他答案状态一样,
JsonConvert.DeserializeObject()
是您需要的方法。之后,您只需要遍历反序列化的数据结构

至于为什么在尝试反序列化之后会得到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);
Rootobject root=JsonConvert.DeserializeObject(数据);
if(root==null)||
root.TrackingInformationResponse==null||
root.TrackingInformationResponse.Shippings==null||
root.TrackingInformationResponse.Shippings.Length==0)
{
抛出新异常();
}
Shipping Shipping=root.TrackingInformationResponse.Shipping[0];
if(shipping.delegator==null | | shipping.delegator.address==null)
{
抛出新异常();
}
Console.WriteLine(“收货人:{0}”,shipping.delegate);
Console.WriteLine(“预计到达时间:{0}”,Shipping.estimatedTimeOfArrival);

大家好。有人能让我理解公共类Rootobject{public Trackinginformationresponse Trackinginformationresponse{get;set;}}公共类Trackinginformationresponse{public-shipping[]shippings{get;set;}请发布你得到的JSON字符串,这样我们可以看到反序列化的输入是什么。嗨,这是信息的样子。我想从中得到的是收货人的信息和估计到达时间。是的,Newtonsoft.JSON是非常感谢你的方法。我不明白我必须要Rootobject root=JsonConvert.DeserializeObject(数据);Shipping Shipping=root.TrackingInformationResponse.Shippings[0];但现在似乎要显示数据了!您好,如果我想使用类Shipping中的项[],您有没有办法向我解释一下该怎么做?因为它是一个数组,我现在无法在文本框中显示它?