c# - 获取 Json 数组大小

标签 c# arrays json amadeus

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

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

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

Json二

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

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

最佳答案

由于您已经对 JSON 进行了反序列化,因此您所要做的就是调用 hotel.results[0].contacts.Count

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

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

关于c# - 获取 Json 数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780317/

相关文章:

c# - 返回类的所有属性,而不是 null c#

javascript - 在vanilla javascript中反转多个单词

java - Jackson:有没有办法在 boolean 反序列化中忽略 0/1?

c# - 邮件 C# 正文中的图像

c# - 如何使用 Nethereum 创建 Ethereum HD 钱包实现

JavaScript 滚动元素在 Chrome 中有效,但在 IE11 中无效

jquery - 如何在 MVC 应用程序中返回 JSON 并循环遍历 jQuery 中返回的 json?

c++ - 在C++11中初始化结构体数组

javascript - 我想展平嵌套的对象数组并将它们转换为一个对象数组

python - 在 python 中标准化复杂的 Json