I tell you that I am consuming a REST service from a third party, when I make the call this returns me a json, the problem is that the json comes in the following way: (Here is a fragment of the JSON)
"GetBookingResult": "{\"reserva\":{\"fecha_creacion\":{\"#text\":\"15FEB17\"},\"hora_creacion\":{\"#text\":\"2133\"},\"responsable\":{\"tipo_reserva\":\"WEBPAS\",\"cod_cia\":\"OB\",\"off_resp\":\"OBW101\"},\"localizador_resiber\":{\"#text\":\"P7S44\"}
This is the code I am using to consume the service
public static Reserva getBooking(GetBookingRequest requestParams)
{
try
{
StreamWriter requestWriter;
string postData = new JavaScriptSerializer().Serialize(requestParams);
var webRequest = System.Net.WebRequest.Create("http://miUrl.svc/metodo") as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json; charset=utf-8";
//POST the data.
using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}
}
using (HttpWebResponse resp = webRequest.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
resp.StatusCode,
resp.StatusDescription));
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
var resultado = reader.ReadToEnd();
Console.WriteLine(resultado);
var str = resultado.Replace(@"\", string.Empty);
JToken token = JToken.Parse(str);
JObject o = JObject.Parse((string)token);
JObject response = o["reserva"] as JObject;
GetBookingResponse respuesta = response.ToObject<GetBookingResponse>();
return respuesta.reserva;
}
}
catch (Exception ex)
{
Console.WriteLine("Ocurrio un error obteniendo los datos de la reserva");
return null;
}
}
in the end what I'm trying to do is get the reserve element of the json that returns the service and convert it to a reservation type object that I created in .NET