JsonObject in .NET for Windows Store App
-
11-12-2019 - |
Question
I get a response string from an API by using this code :
HttpResponseMessage response = await client.GetAsync(url);
string responseText = await response.Content.ReadAsStringAsync();
However I'm unable to find out how to initialize a JsonObject since in .NET for WinRT the constructor JsonObject() doesn't take any arguments. For memory I could have made like that in the "regular" .NET Framework :
JsonObject root = new JsonObject(responseText);
What I've missed ?
Solution
If you want to serialize the response as a JsonObject
you should use JsonObject.Parse(string)
or JsonObject.TryParse(string, out JsonObject)
methods.
OTHER TIPS
Unless you are truly needing to parse/traverse a JSON encoded string, perhaps all you need to is deserialize it. Here are Microsoft docs on doing that.
Deserialize JSON Encoded String
I personally like working with Newtonsoft's JSON API for this task.
MyObject obj = JsonConvert.DeserializeObject<MyObject>(jsonEncodedString);
Hope this helps.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow