2016-07-26 174 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