简体   繁体   中英

How to get values from json string using newtonsoft .json in c#

I have following json string (this is part of it).

[
  {
    "$id": "1",
    "topdealMaster": [
      {
        "$id": "2",
        "topdeal_id": 27,
        "calling_number": "12345678",
        "whats_include": "fssdf\nsdfsd",
        "child_policy": "sdfsdf\nsdf\nsdfsd",
        "optional_extras": "sdfsd\nsdfsd",
        "fine_prints": "sdfsdf\nsdf",
        "prices_id": "54,55,52,53,",
        "hotels_id": "2086,2086,",
        "cruise_id": "23,",
        "tour_id": "28,27,",
        "deal_id": 20224,
        "created_user_id": 28,
        "created_datetime": null,
        "last_modified_user_id": null,
        "last_modified_datetime": null,
        "isActive": null,
        "employee_master": null,
        "deal_master": {
          "$id": "3",
          "deal_id": 20224,
          "our_deal_code": "TCTZ00021",
          "deal_code": "TCTZ00021",
          "deal_company_id": null,
          "deal_title": "Thasnen testing",
          "created_user_id": null,
          "created_time": null,
          "isAlwaysOn": null,
          "isActive": null,
          "activated_user_id": null,
          "activated_time": null,
          "extra_info": null,
          "deal_type_id": "TCTZ",
          "min_price": null,
          "save_up": null,
          "fine_prints": null,
          "acco_type_id": null,
          "deal_hotel_id": null,
          "no_of_nigths": null,
          "airport_code": null,
          "deal_comments": [],
          "deal_fares": [],
          "deal_other_company_hotel": null,
          "topdeal_master": []
        }
      }
    ],
    "topDealHotel": [
      {
        "$id": "4",
        "Deal_Hotel_Id": 2086,
        "Deal_Hotel_Name": "The Ocean Colombo",
        "Created_Emp_Id": 108,
        "Updated_Emp_Id": 28,
        "Updated_Time": null,
        "System_Date_Time": "2016-06-20T10:11:19.037",
        "City_Code": "CMB",
        "Latitude": 6.88531,
        "Longitude": 79.855195,
        "LocationId": 6887404,
        "Hotel_Amentities": [
          {
            "$id": "5",
            "id": 0,
            "hotel_amenity_id": 2,
            "amenity_type": "24-hour front desk",
            "image_path": "fa fa-square"
          },
          {
            "$id": "6",
            "id": 0,
            "hotel_amenity_id": 3,
            "amenity_type": "Airport transportation",
            "image_path": "fa fa-square"
          },
          {
            "$id": "7",
            "id": 0,
            "hotel_amenity_id": 8,
            "amenity_type": "Bar/lounge",
            "image_path": "fa fa-square"
          },
          {
            "$id": "8",
            "id": 0,
            "hotel_amenity_id": 9,
            "amenity_type": "Breakfast available (surcharge)",
            "image_path": "fa fa-square"
          },
          {
            "$id": "9",
            "id": 0,
            "hotel_amenity_id": 10,
            "amenity_type": "Business center",
            "image_path": "fa fa-square"
          },
          {
            "$id": "10",
            "id": 0,
            "hotel_amenity_id": 16,
            "amenity_type": "Dry cleaning/laundry service",
            "image_path": "fa fa-square"
          },
          {
            "$id": "11",
            "id": 0,
            "hotel_amenity_id": 17,
            "amenity_type": "Elevator/lift",
            "image_path": "fa fa-square"
          },
          {
            "$id": "12",
            "id": 0,
            "hotel_amenity_id": 22,
            "amenity_type": "Free newspapers in lobby",
            "image_path": "fa fa-square"
          },
          {
            "$id": "13",
            "id": 0,
            "hotel_amenity_id": 23,
            "amenity_type": "Free WiFi",
            "image_path": "fa fa-square"
          },
          {
            "$id": "14",
            "id": 0,
            "hotel_amenity_id": 32,
            "amenity_type": "Limo or Town Car service available",
            "image_path": "fa fa-square"
          },
          {
            "$id": "15",
            "id": 0,
            "hotel_amenity_id": 33,
            "amenity_type": "Luggage storage",
            "image_path": "fa fa-square"
          },
          {
            "$id": "16",
            "id": 0,
            "hotel_amenity_id": 39,
            "amenity_type": "Restaurant",
            "image_path": "fa fa-square"
          },
          {
            "$id": "17",
            "id": 0,
            "hotel_amenity_id": 43,
            "amenity_type": "Smoke-free property",
            "image_path": "fa fa-square"
          }
        ],
        "Room_Amentities": [
          {
            "$id": "18",
            "id": 0,
            "room_amenity_id": 2,
            "amenity_type": "Air conditioning",
            "ImagePath": "fa fa-square"
          },
          {
            "$id": "19",
            "id": 0,
            "room_amenity_id": 4,
            "amenity_type": "Blackout drapes/curtains",
            "ImagePath": "fa fa-square"
          },

How can I get value(separate objects and value) from this string using newtonsoft .json or any other way.

I tried like this(by creating root objects , just a small try)

 public class RootobjectOne
        {
            [JsonProperty("topdealMaster")]
            public TopdealMaster TopdealMaster { get; set; }
        }

        public class TopdealMaster
        {
            [JsonProperty("topdeal_id")]
            public string topdealId {get;set;}

        }

and then calle for it like this

string b = client.GetPromotionalTopDeal_TOUR("TCTZ00021"); // this gives the json string

and try with this, calling to above class.

RootobjectOne one = JsonConvert.DeserializeObject<RootobjectOne>(b);

but didn't get any succesful result.hope your help with this.

note : it gives following error ----- > because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

You have an array on top level, so it should be:

var items = JsonConvert.DeserializeObject<List<RootobjectOne>>(b);

Also "topdealMaster" is an array, so the root class should be something like:

public class RootobjectOne
{
    [JsonProperty("topdealMaster")]
    public Topdealmaster[] TopdealMasters { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM