string json = client.DownloadString(uri);
JavaScriptSerializer jsonSerialization = new JavaScriptSerializer();
var res = jsonSerialization.Serialize(json);
var result = jsonSerialization.DeserializeObject(res);
MyLiteral.Text = result["Name"];
I'm getting data in json and then after serializing and deserializing, I'm trying to extract some value from json object but getting errors. Please help
CodePudding user response:
I think you should create a dumy class that contains the names you want to de-serialze. You only need to put those properties in the dummy class you actually want to use and not all of them e. g.
public DummyClass {
public string Name {get;set;}
}
and then you need to adapt your code in these lines:
var result = jsonSerialization.DeserializeObject<DummyClass>(res);
MyLiteral.Text = result.Name;
CodePudding user response:
void Main()
{
// Using Newtonsoft.Json
var result = " {\"Distance\":0.0,\"BookingLink\":null,\"Images\":null,\"HotelID\":105304,\"Name\":\"Hyatt Regency Century Plaza\",\"AirportCode\": \"\"}";
var hotel = JsonConvert.DeserializeObject<Hotel>(result);
Console.WriteLine(hotel.Name);
}
// Create the object class structure based on your JSON string results
public class Hotel
{
public double Distance { get; set; }
public string BookingLink { get; set; }
public object Images { get; set; }
public int HotelID { get; set; }
public string Name { get; set; }
public string AirportCode { get; set; }
}