2016-07-26 188 views
0

我有以下json字符串(這是它的一部分)。如何從json字符串中使用newtonsoft .json獲取值c#

[ 
    { 
    "$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" 
      }, 

我怎樣才能從這個字符串中使用newtonsoft以.json或任何其他方式的值(單獨的對象和值)。

我想是這樣的(通過創建根對象,只是一個小試)

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

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

     } 

,然後卡萊它這樣

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

以及與此嘗試,打電話給上面的類。

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

但是沒有得到任何成功的結果。請您對此有所幫助。

注意:由於該類型需要JSON對象(例如{「name」:「value」})才能正確反序列化,所以會出現以下錯誤----->。 要修復此錯誤,請將JSON更改爲JSON對象(例如{「name」:「value」})或將反序列化類型更改爲實現集合接口(例如ICollection,IList)的數組或類型,如List可以從JSON數組中反序列化。 JsonArrayAttribute也可以添加到類型中,以強制它從JSON數組反序列化。

+0

的http:// stackoverflow.com/questions/9010021/get-value-from-json-with-json-net – niksofteng

回答

1

你有頂級的數組,所以它應該是:

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

而且"topdealMaster"是一個數組,所以根類應該是這樣的:

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

不適合我 – bill

+0

根對象也應該改變 –

+0

工作完成和thanx – bill