【问题标题】:Get only one property from Json in deserialize (C#)在反序列化(C#)中仅从 Json 获取一个属性
【发布时间】:2020-09-01 12:07:43
【问题描述】:

我有来自 API 的 json

这是其中的一部分

 {
  "Area": "Werrington, Peterborough",
  "MetaData": {
    "CanonicalName": "pe4-werrington,peterborough",
    "District": "PE4",
    "Postcode": "PE46QR",
    "Area": "Werrington, Peterborough",
    "Latitude": 52.609645,
    "Longitude": -0.268349,
    "ResultCount": 113,
    "SearchedTerms": null
  },
  "Restaurants": [
    {
      "Id": 65016,
      "Name": "Grill & Krispy",
      "UniqueName": "Grill-and-Krispy-pe1",
      "Address": {
        "City": "Peterborough",
        "FirstLine": "20 Westgate",
        "Postcode": "PE1 2SY",
        "Latitude": 52.574544,
        "Longitude": -0.243271
      },
      "City": "Peterborough",
      "Postcode": "PE1 2SY",
      "Latitude": 0.0,
      "Longitude": 0.0,
      "Rating": {
        "Count": 1726,
        "Average": 4.23,
        "StarRating": 4.23
      },
      "RatingStars": 4.23,
      "NumberOfRatings": 1726,
      "RatingAverage": 4.23,
      "Description": "",
      "Url": "https://www.just-eat.co.uk/restaurants-Grill-and-Krispy-pe1",
      "LogoUrl": "http://d30v2pzvrfyzpo.cloudfront.net/uk/images/restaurants/65016.gif",
      "IsTestRestaurant": false,
      "IsHalal": false,
      "IsNew": false,
      "ReasonWhyTemporarilyOffline": "",
      "DriveDistance": 2.6,
      "DriveInfoCalculated": true,
      "IsCloseBy": false,
      "OfferPercent": 20.0,
      "NewnessDate": "2016-11-03T12:58:00",
      "OpeningTime": "2020-09-02T09:30:00Z",
      "OpeningTimeUtc": null,
      "OpeningTimeIso": "2020-09-02T09:30:00",
      "OpeningTimeLocal": "2020-09-02T10:30:00",
      "DeliveryOpeningTimeLocal": "2020-09-01T12:00:00",
      "DeliveryOpeningTime": "2020-09-01T11:00:00Z",
      "DeliveryOpeningTimeUtc": null,
      "DeliveryStartTime": "2020-09-01T11:00:00Z",
      "DeliveryTime": null,
      "DeliveryTimeMinutes": null,
      "DeliveryWorkingTimeMinutes": 40,
      "DeliveryEtaMinutes": {
        "Approximate": null,
        "RangeLower": 30,
        "RangeUpper": 45
      },
      "IsCollection": true,
      "IsDelivery": true,
      "IsFreeDelivery": false,
      "IsOpenNowForCollection": true,
      "IsOpenNowForDelivery": true,
      "IsOpenNowForPreorder": false,
      "IsOpenNow": true,
      "IsTemporarilyOffline": false,
      "DeliveryMenuId": 230763,
      "CollectionMenuId": null,
      "DeliveryZipcode": null,
      "DeliveryCost": 0.0,
      "MinimumDeliveryValue": 0.0,
      "SecondDateRanking": 0.0,
      "DefaultDisplayRank": 0,
      "SponsoredPosition": 0,
      "SecondDateRank": 0.0,
      "Score": 113.0,
      "IsTemporaryBoost": false,
      "IsSponsored": true,
      "IsPremier": false,
      "HygieneRating": null,
      "ShowSmiley": false,
      "SmileyDate": null,
      "SmileyElite": false,
      "SmileyResult": null,
      "SmileyUrl": null,
      "SendsOnItsWayNotifications": false,
      "BrandName": "",
      "IsBrand": false,
      "LastUpdated": "2020-09-01T11:16:19.144899"
    }
  ]
}

我只需要获得餐厅部分。

这是我尝试这样做的方法

var baseUrl = "**********";
        var client = new RestClient(baseUrl);
        var request = new RestRequest($"restaurants/bypostcode/{input.PostCode}", Method.GET);
        var response = await client.ExecuteTaskAsync(request);
        var searchResult = JsonConvert.DeserializeObject<Restaurant>(response.Content);

在反序列化时我得到了这个

{
"id": 0,
"name": null,
"uniqueName": null,
"address": null,
"city": null,
"postcode": null,
"latitude": 0,
"longitude": 0,
"rating": null,
"ratingStars": 0,
"numberOfRatings": 0,
"ratingAverage": 0,
"description": null,
"url": null,
"logoUrl": null,
"isTestRestaurant": false,
"isHalal": false,
"isNew": false,
"reasonWhyTemporarilyOffline": null,
"driveDistance": 0,
"driveInfoCalculated": false,
"isCloseBy": false,
"offerPercent": 0,
"newnessDate": "0001-01-01T00:00:00",
"openingTime": "0001-01-01T00:00:00",
"openingTimeUtc": null,
"openingTimeIso": "0001-01-01T00:00:00",
"openingTimeLocal": "0001-01-01T00:00:00",
"deliveryOpeningTimeLocal": "0001-01-01T00:00:00",
"deliveryOpeningTime": "0001-01-01T00:00:00",
"deliveryOpeningTimeUtc": null,
"deliveryStartTime": "0001-01-01T00:00:00",
"deliveryTime": null,
"deliveryTimeMinutes": null,
"deliveryWorkingTimeMinutes": 0,
"isCollection": false,
"isDelivery": false,
"isFreeDelivery": false,
"isOpenNowForCollection": false,
"isOpenNowForDelivery": false,
"isOpenNowForPreorder": false,
"isOpenNow": false,
"isTemporarilyOffline": false,
"deliveryMenuId": 0,
"collectionMenuId": null,
"deliveryZipcode": null,
"deliveryCost": 0,
"minimumDeliveryValue": 0,
"secondDateRanking": 0,
"defaultDisplayRank": 0,
"sponsoredPosition": 0,
"secondDateRank": 0,
"score": 0,
"isTemporaryBoost": false,
"isSponsored": false,
"isPremier": false,
"hygieneRating": null,
"showSmiley": false,
"smileyDate": null,
"smileyElite": false,
"smileyResult": null,
"smileyUrl": null,
"sendsOnItsWayNotifications": false,
"brandName": null,
"isBrand": false,
"lastUpdated": "0001-01-01T00:00:00",
"tags": null,
"deliveryChargeBands": null,
"badges": null,
"openingTimes": null,
"serviceableAreas": null

}

我该如何解决这个问题?

【问题讨论】:

  • 你不能只反序列化一部分数据,你可以尝试JsonPath查询来检索想要的元素然后反序列化它

标签: c# .net asp.net-core json.net


【解决方案1】:

您要反序列化的 JSON 不是 Restaurant 的实例。它是一个对象的实例,它有一个 称为 Restaurants 的属性,它是一个可能是 Restaurant 对象的数组。

创建该对象结构:

public class RestaurantsInfo
{
    public IEnumerable<Restaurant> Restaurants { get; set; }
}

然后反序列化:

var searchResult = JsonConvert.DeserializeObject<RestaurantsInfo>(response.Content);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多