Reputation: 117
I am currently working on migrating a WPF application to UWP. One of its functions is text translation using the Microsoft Azure Text Translation API. In WPF, I used NewtonSoft.Json and in UWP, I am trying to use Windows.Data.Json. The API is returning the following string: "[\" [{\ "" translations \\ ": [{\\" text \\ "": \ "Un travail lourd. \\", \\ "to \\" : \\ "fr \\"}]}] \ "]" and the code below returns an invalid Json string error when trying to execute the Parse method. Any idea what is going on? Any help is greatly appreciated.
var response = client.SendAsync(request).Result;
var responseBody = response.Content.ReadAsStringAsync().Result;
JsonArray jsonArray = new JsonArray();
jsonArray.Add(JsonValue.CreateStringValue(responseBody));
string str = jsonArray.Stringify();
JsonObject job = new JsonObject();
job = JsonObject.Parse(str);
var model = new JsonT
{
TextT = job.GetNamedString("text")
};
Upvotes: 0
Views: 385
Reputation: 10401
You probably just need to use JsonObject.Parse directly:
var response = client.SendAsync(request).GetAwaiter().GetResult();
var responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var job = JsonObject.Parse(responseBody);
var model = new JsonT
{
TextT = job.GetNamedArray("translations").GetAt(0).GetString("text")
};
Though as it has been pointed, https://www.newtonsoft.com/json is a more widely used library, so you may want to use it instead, as there are more tutorials/questions dealing with its use than with UWP Json classes.
Upvotes: 1