Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing json objects

I'm having trouble understanding how to parse JSON string into c# objects with Visual .NET. The task is very easy, but I'm still lost... I get this string:

{"single_token":"842269070","username":"example123","version":1.1}

And this is the code where I try to desterilize:

namespace _SampleProject
{
    public partial class Downloader : Form
    {
        public Downloader(string url, bool showTags = false)
        {
            InitializeComponent();
            WebClient client = new WebClient();
            string jsonURL = "http://localhost/jev";   
            source = client.DownloadString(jsonURL);
            richTextBox1.Text = source;
            JavaScriptSerializer parser = new JavaScriptSerializer();
            parser.Deserialize<???>(source);
        }

I don't know what to put between the '<' and '>', and from what I've read online I have to create a new class for it..? Also, how do I get the output? An example would be helpful!

like image 912
Gregor Menih Avatar asked May 03 '12 13:05

Gregor Menih


People also ask

What happens if you JSON parse an object?

JSON.parse() The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

How do you parse an array of JSON objects?

Example - Parsing JSON Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

What is JSON parsing example?

JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.

How does JSON parsing work?

How Does JSON Parse Work? The JSON parse function takes data that's in a text format and then converts it into JavaScript. Usually, these take the form of JavaScript objects, but the parse function can be used directly on arrays as well.


1 Answers

Create a new class that your JSON can be deserialized into such as:

public class UserInfo
{
    public string single_token { get; set; }
    public string username { get; set; }
    public string version { get; set; }
}

public partial class Downloader : Form
{
    public Downloader(string url, bool showTags = false)
    {
        InitializeComponent();
        WebClient client = new WebClient();
        string jsonURL = "http://localhost/jev";
        source = client.DownloadString(jsonURL);
        richTextBox1.Text = source;
        JavaScriptSerializer parser = new JavaScriptSerializer();
        var info = parser.Deserialize<UserInfo>(source);

        // use deserialized info object
    }
}
like image 172
jaryd Avatar answered Nov 06 '22 12:11

jaryd