4

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

Thanks for every members here who helped.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Yih Wei
  • 537
  • 3
  • 21
  • @john I receive an error of Non-invocable member cannot be used like a method. – Yih Wei Jun 10 '18 at 03:35
  • So where is `hotel` to match `hotel.results[0].contacts`? The only object I see in the JSON root level is `results`. Also, I don't think you can just navigate the JSON objects with one string like that. I think you need `(JArray)jObject["results"][0]["contacts"];` – ProgrammingLlama Jun 10 '18 at 03:35
  • @john I have `Hotel hotel = Newtonsoft.Json.JsonConvert.DeserializeObject(json);`. So, I have to use `JArray jArray = (JArray)jObject["results[0].contacts"];` instead of `JArray jArray = (JArray)jObject["hotel.results[0].contacts"];`? – Yih Wei Jun 10 '18 at 03:38
  • I edited my comment, but Lionel's answer makes most sense considering you have already deserialized the data into C# objects. – ProgrammingLlama Jun 10 '18 at 03:39
  • @john Your answer (JArray)jObject["results"][0]["contacts"]; is working. I have accepted Lionel's answer as you suggest it to makes most sense. Thanks for your answer too. – Yih Wei Jun 10 '18 at 03:55

1 Answers1

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.

Guybrush
  • 256
  • 4
  • 16
  • Thanks for your answer. I receive an error of Error CS1061 'List' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?) – Yih Wei Jun 10 '18 at 03:46
  • Instead of using length, please use `hotel.results[0].contacts.Count` instead. Length will only work with the array data type. I have updated my answer to reflect this. – Guybrush Jun 10 '18 at 03:50
  • Good to hear! Glad I could help. – Guybrush Jun 10 '18 at 03:56