Reputation: 897
I am getting unexpected parse error while trying to read the below json value.This is the response that i get after calling an external API
"{
status: 201,
data: {
booking_id: B2B-E51771039A176C,
booking_amount: 5398.00,
room_charges: 5398.00,
meal_charges: 0.00,
inclusion_charges: 600.00,
taxes: 1133.58,
status: Initiated
}
}"
I want to get the booking_id from the above json in a string.
Here is the model that i generated online
public class Data
{
[JsonProperty("booking_id")]
public string booking_id { get; set; }
[JsonProperty("booking_amount")]
public string booking_amount { get; set; }
[JsonProperty("room_charges")]
public string room_charges { get; set; }
[JsonProperty("meal_charges")]
public string meal_charges { get; set; }
[JsonProperty("inclusion_charges")]
public string inclusion_charges { get; set; }
[JsonProperty("taxes")]
public string taxes { get; set; }
[JsonProperty("status")]
public string status { get; set; }
}
public class RootObject
{
public int status { get; set; }
public Data data { get; set; }
}
And this is what i have used to convert
RootObject rootobj=JsonConvert.DeserializeObject<RootObject>(JsonReplace);
Upvotes: 0
Views: 161
Reputation: 27049
That is not valid JSON because in JSON a string value needs to be in quotes or else it will be treated as numeric. During deserialization the deserializer will attempt to treat the value as number and it will fail.
This is the valid json:
{
status: 201,
data: {
booking_id: "B2B-E51771039A176C",
booking_amount: 5398.00,
room_charges: 5398.00,
meal_charges: 0.00,
inclusion_charges: 600.00,
taxes: 1133.58,
status:"Initiated"
}
}
Notice the quotations around booking_id
and status
.
This valid json will have a class like this:
public class Data
{
public string booking_id { get; set; }
public double booking_amount { get; set; }
public double room_charges { get; set; }
public double meal_charges { get; set; }
public double inclusion_charges { get; set; }
public double taxes { get; set; }
public string status { get; set; }
}
public class RootObject
{
public int status { get; set; }
public Data data { get; set; }
}
Get the value like this:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
var id = obj.data.booking_id;
Upvotes: 1
Reputation: 4632
It looks to me that it is not valid, I think it's because of the booking_id
and status
. The data property for booking_id
and status
are not string, so I think it's the problem. You should pass it as string (with quotes):
"{
status: 201,
data: {
booking_id: "B2B-E51771039A176C",
booking_amount: 5398.00,
room_charges: 5398.00,
meal_charges: 0.00,
inclusion_charges: 600.00,
taxes: 1133.58,
status: "Initiated"
}
}"
Hope it helps!
Upvotes: 1
Reputation: 1486
Your JSON is invalid. Your booking_id value should have string in double quotes and status text also.
{
status: 201,
data: {
booking_id: "B2B-E51771039A176C",
booking_amount: 5398.00,
room_charges: 5398.00,
meal_charges: 0.00,
inclusion_charges: 600.00,
taxes: 1133.58,
status: "Initiated"
}
}
Upvotes: 1