Does anyone know how to convert a string which contains json into a C# array. I have this which reads the text/json from a webBrowser and stores it into a string.
string docText = webBrowser1.Document.Body.InnerText;
Just need to somehow change that json string into an array. Been looking at Json.NET but I'm not sure if that's what I need, as I don't want to change an array into json; but the other way around. Thanks for the help!
just take the string and use the JavaScriptSerializer to deserialize it into a native object. For example, having this json:
You'd need to create a C# class called, for example, Person defined as so:
You can now deserialize the JSON string into an array of Person by doing:
Here's a link to JavaScriptSerializer documentation.
Note: my code above was not tested but that's the ideaTested it. Unless you are doing something "exotic", you should be fine using the JavascriptSerializer.Install this class in package console This class works fine in all .NET Versions, for example in my project: I have DNX 4.5.1 and DNX CORE 5.0 and everything works.
Firstly before JSON deserialization, you need to declare a class to read normally and store some data somewhere This is my class:
In HttpContent section where you requesting data by GET request for example:
Yes, Json.Net is what you need. You basically want to deserialize a Json string into an array of
objects
.See their examples: