代码之家  ›  专栏  ›  技术社区  ›  Yih Wei

获取json数组大小

  •  3
  • Yih Wei  · 技术社区  · 6 年前

    我有两个从web api返回的json,其中包含动态大小数组“contacts”和“facilities”。

    通常,我将使用以下代码访问json中的固定大小数组“results”,因为我是固定大小的。

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

    我已经试过了 Link 下面的代码可以得到动态数组的大小,但它似乎不起作用。

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

    谢谢这里所有帮忙的人。

    1 回复  |  直到 6 年前
        1
  •  4
  •   Guybrush    6 年前

    因为您已经在反序列化json,所以您只需调用 hotel.results[0].contacts.Count .

    这将返回一个整数,表示联系人列表中的项目数。

    如果 contacts 是数组数据类型,需要使用 length 而不是 count .