Yih Wei
Yih Wei

Reputation: 537

Get Json Array Size

I have Two Json returned from Web API which contain dynamic size array "contacts" and "amenities".

Normally I will access the fixed size array "results" in Json with the following code as I am the on fix the size.

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

I have tried the following Link and following code to get the dynamic array size back but it don't seem to be working.

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

Json One

{
  "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"
        }
      }
    }
  ]
}

Json Two

{
  "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": "[email protected]"
        },
        {
          "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"
        }
      }
    }
  ]
}

Thanks for every members here who helped.

Upvotes: 4

Views: 6256

Answers (1)

Guybrush
Guybrush

Reputation: 256

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.

Upvotes: 5

Related Questions