4

我有两个从 Web API 返回的 Json,其中包含动态大小数组“联系人”和“设施”。

通常我将使用以下代码访问 Json 中固定大小的数组“结果”,因为我正在修复大小。

string json = _obj.GetJsonResult(url);
Hotel hotel = Newtonsoft.Json.JsonConvert.DeserializeObject<Hotel>(json);
hotelName = hotel.results[0].property_name;

我尝试了以下链接和以下代码来获取动态数组大小,但它似乎不起作用。

var jObject = JObject.Parse(json);
JArray jArray = (JArray)jObject["hotel.results[0].contacts"];
int length = jArray.Count;

杰森一号

{
  "results": [
    {
      "property_code": "FGPENOTP",
      "property_name": "Olive Tree Penang",
      "location": {
        "latitude": 5.32708,
        "longitude": 100.27944
      },
      "address": {
        "line1": "No 76 Jalan Mahsuri",
        "city": "Bayan Lepas",
        "postal_code": "11950",
        "country": "MY"
      },
      "total_price": {
        "amount": "265.00",
        "currency": "MYR"
      },
      "min_daily_rate": {
        "amount": "250.00",
        "currency": "MYR"
      },
      "contacts": [
        {
          "type": "PHONE",
          "detail": "+6046377777"
        },
        {
          "type": "FAX",
          "detail": "+6046377788"
        }
      ],
      "amenities": [
        {
          "amenity": "RESTAURANT",
          "ota_code": 76,
          "description": "Restaurant"
        },
        {
          "amenity": "ELEVATORS",
          "ota_code": 33,
          "description": "Elevators"
        },
        {
          "amenity": "LAUNDRY_SERVICE",
          "ota_code": 58,
          "description": "Laundry/Valet service"
        },
        {
          "amenity": "PARKING",
          "ota_code": 68,
          "description": "Parking"
        },
        {
          "amenity": "ROOM_SERVICE",
          "ota_code": 77,
          "description": "Room service"
        },
        {
          "amenity": "SAFE_DEPOSIT_BOX",
          "ota_code": 78,
          "description": "Safe deposit box"
        },
        {
          "amenity": "OUTDOOR_POOL",
          "ota_code": 66,
          "description": "Outdoor pool"
        },
        {
          "amenity": "BALLROOM",
          "ota_code": 191,
          "description": "Ballroom"
        }
      ],
      "awards": [
        {
          "provider": "Local Star Rating",
          "rating": "4"
        }
      ],
      "images": [],
      "rooms": [
        {
          "booking_code": "A1KBAR",
          "room_type_code": "A1K",
          "rate_plan_code": "BAR",
          "total_amount": {
            "amount": "265.00",
            "currency": "MYR"
          },
          "rates": [
            {
              "start_date": "2018-06-15",
              "end_date": "2018-06-16",
              "currency_code": "MYR",
              "price": 250
            }
          ],
          "descriptions": [
            "BEST AVAILABLE RATE",
            "DELUXE ROOM/ KING OR TWIN SIZED BED/ 28 SQM/",
            "FREE WI FI"
          ],
          "room_type_info": {
            "room_type": "Room with bath - Superior",
            "bed_type": "King",
            "number_of_beds": "1"
          },
          "rate_type_code": "BAR"
        }
      ],
      "_links": {
        "more_rooms_at_this_hotel": {
          "href": "https://api.sandbox.amadeus.com/v1.2/hotels/FGPENOTP?apikey=pa3lM5UhJAZ0AJfzlZHcAKdiGVBA90wA&check_in=2018-06-15&check_out=2018-06-16&lang=EN¤cy=MYR&referrer=more_rooms_at_this_hotel"
        }
      }
    }
  ]
}

杰森二

{
  "results": [
    {
      "property_code": "WWKULAPT",
      "property_name": "Sama Sama Hotel Klia",
      "location": {
        "latitude": 2.75803,
        "longitude": 101.7041
      },
      "address": {
        "line1": "Jalan Cta 4b",
        "city": "Selangor",
        "postal_code": "64000",
        "country": "MY"
      },
      "total_price": {
        "amount": "625.00",
        "currency": "MYR"
      },
      "min_daily_rate": {
        "amount": "625.00",
        "currency": "MYR"
      },
      "contacts": [
        {
          "type": "PHONE",
          "detail": "60-3-87873333"
        },
        {
          "type": "FAX",
          "detail": "60-3-87875555"
        },
        {
          "type": "EMAIL",
          "detail": "info@samasamahotels.com"
        },
        {
          "type": "URL",
          "detail": "www.samasamahotels.com"
        }
      ],
      "amenities": [
        {
          "amenity": "COFFEE_SHOP",
          "ota_code": 20,
          "description": "Coffee shop"
        },
        {
          "amenity": "RESTAURANT",
          "ota_code": 76,
          "description": "Restaurant"
        },
        {
          "amenity": "ACCESSIBLE_FACILITIES",
          "ota_code": 47,
          "description": "Accessible facilities"
        },
        {
          "amenity": "BEAUTY_SALON",
          "ota_code": 107,
          "description": "Beauty shop/salon"
        },
        {
          "amenity": "CAR_RENTAL",
          "ota_code": 15,
          "description": "Car rental desk"
        },
        {
          "amenity": "CHILDREN_WELCOME",
          "ota_code": 218,
          "description": "Children welcome"
        },
        {
          "amenity": "ELEVATORS",
          "ota_code": 33,
          "description": "Elevators"
        },
        {
          "amenity": "CURRENCY_EXCHANGE",
          "ota_code": 26,
          "description": "Currency exchange"
        },
        {
          "amenity": "EXECUTIVE_FLOOR",
          "ota_code": 34,
          "description": "Executive floor"
        },
        {
          "amenity": "HOTSPOTS",
          "ota_code": 221,
          "description": "Hotspots"
        },
        {
          "amenity": "INTERNET_PUBLIC_AREAS",
          "ota_code": 178,
          "description": "Internet access in public areas"
        },
        {
          "amenity": "JACUZZI",
          "ota_code": 55,
          "description": "Jacuzzi"
        },
        {
          "amenity": "LAUNDRY_SERVICE",
          "ota_code": 58,
          "description": "Laundry/Valet service"
        },
        {
          "amenity": "MASSAGE_SERVICES",
          "ota_code": 61,
          "description": "Massage services"
        },
        {
          "amenity": "NON_PORNOGRAPHIC_TV",
          "ota_code": 220,
          "description": "Hotel does not provide pornographic films/TV"
        },
        {
          "amenity": "PARKING",
          "ota_code": 68,
          "description": "Parking"
        },
        {
          "amenity": "SAUNA",
          "ota_code": 79,
          "description": "Sauna"
        },
        {
          "amenity": "SPA",
          "ota_code": 84,
          "description": "Spa"
        },
        {
          "amenity": "VALET_PARKING",
          "ota_code": 97,
          "description": "Valet parking "
        },
        {
          "amenity": "",
          "ota_code": 46,
          "description": "Hairdresser/barber"
        },
        {
          "amenity": "POOL",
          "ota_code": 71,
          "description": "Pool"
        },
        {
          "amenity": "ROOM_SERVICE",
          "ota_code": 77,
          "description": "Room service"
        },
        {
          "amenity": "SAFE_DEPOSIT_BOX",
          "ota_code": 78,
          "description": "Safe deposit box"
        }
      ],
      "awards": [
        {
          "provider": "OHG",
          "rating": "Deluxe"
        }
      ],
      "images": [],
      "rooms": [
        {
          "booking_code": "DR12ZZZ",
          "room_type_code": "D2T",
          "rate_plan_code": "PRO",
          "total_amount": {
            "amount": "625.00",
            "currency": "MYR"
          },
          "rates": [
            {
              "start_date": "2018-06-15",
              "end_date": "2018-06-16",
              "currency_code": "MYR",
              "price": 625
            }
          ],
          "descriptions": [
            "Sama Sama Best Available Rate GDS",
            "Deluxe Twin/AC/Free WiFi/LCD TV/36SQM/",
            "NonSmoking/Work Desk/Safe/Minibar"
          ],
          "room_type_info": {
            "room_type": "Room with bath - Minimum",
            "bed_type": "Twin",
            "number_of_beds": "2"
          },
          "rate_type_code": "PRO"
        }
      ],
      "_links": {
        "more_rooms_at_this_hotel": {
          "href": "https://api.sandbox.amadeus.com/v1.2/hotels/WWKULAPT?apikey=pa3lM5UhJAZ0AJfzlZHcAKdiGVBA90wA&check_in=2018-06-15&check_out=2018-06-16&lang=EN¤cy=MYR&referrer=more_rooms_at_this_hotel"
        }
      }
    }
  ]
}

感谢这里帮助过的每一位成员。

4

1 回答 1

5

Since you are already deserializing your JSON, all you should have to do is call hotel.results[0].contacts.Count.

This will return an integer representing the amount of items in the contacts list.

If contacts is of the array datatype, you will need to use length instead of count.

于 2018-06-10T03:35:31.987 回答