2016-07-26 11 views
0

Ich habe folgende JSON-String (das ist ein Teil davon).Wie bekomme ich Werte von JSON-String mit newtonsoft .json in 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" 
      }, 

Wie kann ich Wert (separate Objekte und Wert) aus dieser Zeichenfolge newtonsoft .json oder andere Art und Weise verwendet wird.

ich so versucht (root Erstellen von Objekten, nur ein kleiner Versuch)

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

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

     } 

und dann für sie wie diese mit diesem

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

und versuchen calle, die über Klasse aufrufen.

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

aber haben kein erfolgreiches result.hope Ihre Hilfe dabei.

Hinweis: Es gibt folgenden Fehler ----->, da der Typ erfordert, ein JSON-Objekt (z. B. {"name": "Wert"}) korrekt deserialisieren. Um diesen Fehler zu beheben, ändern Sie den JSON entweder in ein JSON-Objekt (z. B. {"name": "Wert") oder ändern Sie den deserialisierten Typ in ein Array oder einen Typ, der eine Auflistungsschnittstelle implementiert (z. B. ICollection, IList) kann aus einem JSON-Array deserialisiert werden. JsonArrayAttribute kann auch zum Typ hinzugefügt werden, um zu erzwingen, dass er aus einem JSON-Array deserialisiert wird.

+0

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

Antwort

1

Sie haben ein Array auf oberster Ebene, so soll es sein:

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

Auch "topdealMaster" ist ein Array, so dass die Wurzelklasse sollte wie etwas sein:

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

funktioniert nicht für mich – bill

+0

root-Objekt sollte auch geändert werden –

+0

Arbeit erledigt und danke – bill

Verwandte Themen