开始,我是初学者。我一直在寻找看起来像这个问题的东西,而没有设法解决问题。而现在我一直坚持这个问题。我正在使用本地邮寄的API,我们可以在json链接中获取交货时间,地址等信息。但在这种情况下,json看起来有点不同,我不知道如何使用它。
如果有人能向我解释如何访问交付日期和地址等数据,我会非常高兴。现在,当我尝试时,它只显示值为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; }
}
{
"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 :(得分:2)
这是一个可能对您有帮助的现有帖子。
Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
这里有关于牛顿软件的一些信息。 http://www.newtonsoft.com/json/help/html/deserializeobject.htm
使用示例:
Newtonsoft.Json.JsonConvert.DeserializeObject()
答案 1 :(得分:0)
您可以尝试使用
var jarray = JsonConvert.DeserializeObject<Rootobject>(date,
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });
答案 2 :(得分:0)
与其他答案状态一样,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);