C # target ARM: how to deserialize JSON?

I am writing my first C # application for Windows 10 IoT. Everything works for me, my web requests, etc., And I can get the desired content for rendering for the user. But ... this is in JSON, and I cannot represent it in a regular variable so that I can only select one property.

JSON Result
{  
   "date":"2017-07-09",
   "period":"year",
   "views":970347,
   "visitors":150013,
   "likes":136,
   "reblogs":13,
   "comments":1099,
   "followers":145
}

I tried adding the Nuget package for NewtonSoft JSON (JSON.NET) and it does not seem to work on rPI ARM CPU. So I started using the class Windows.Data.JSON.JsonObject.

I am analyzing the JSON response for my application using JSonObject.Parse()with this line, and then when I debug, I see the following.

JsonObject meme = JsonObject.Parse(ResponseContent);

enter image description here

I can see all the values ​​that I want there, under meme. JSonObject offers methods GetNamedString()and GetNamedObject(), but I get completion errors for both:{"A method was called at an unexpected time.\r\n\r\nThis is not a string value. Use ValueType property to get the type."}

PowerShell, $response / $Response.Views, , #. , , , , , JSON .

- Followers and Visitors JSON , . webrequest... JSON Parse.

+4
2

JSON Watch , JsonObject, .

, , , , GetNamedString() GetNamedObject(). GetNamedValue(), :

meme.GetNamedValue("views").GetNumber()

:

meme["views"].GetNumber()

, JSON, meme["whateverJSONkey"].ValueType switch/case

+4

, JSON.NET , ( ):

string s = @"{  
  ""date"":""2017-07-09"",
  ""period"":""year"",
  ""views"":970347,
  ""visitors"":150013,
  ""likes"":136,
  ""reblogs"":13,
  ""comments"":1099,
  ""followers"":145
}";

JsonObject data = null;
if (!JsonObject.TryParse(s, out data))
  new MessageDialog("Not valid JSON").ShowAsync();

var followers = data["followers"].GetNumber();
var visitors = data["visitors"].GetNumber();

new MessageDialog(String.Format("You had {0} visitors and {1} followers",
  visitors, followers)).ShowAsync();
+2

Source: https://habr.com/ru/post/1681081/


All Articles