R
R
Renat Iliev2014-03-08 22:38:52
JSON
Renat Iliev, 2014-03-08 22:38:52

JSON in C#, how to deserialize and use?

There is, for example, this line:

{
    "status": "ok",
    "count": 1,
    "data": [
        {
            "nickname": "The_IzeBerg",
            "account_id": 11545443
        }
    ]
}

I need to pull account_id from there.
How to do it? (I'm a complete C# noob, so don't throw your slippers around.)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kokcuk, 2014-03-08
@Kokcuk

1) Download dll james.newtonking.com/json
from here 2) Connect to the project
3) Write JObject o = JObject.Parse(jsonString);
4) Get o["data"]["account_id"] (like this)

A
Alexander, 2014-03-09
@avorsa

Absolutely for beginners mustknowthat.blogspot.ru/2013/06/json-c.html?m=1

A
AlexP11223, 2014-03-09
@AlexP11223

Use the library james.newtonking.com/json You
can connect the library through NuGet: RMB on References, Manage NuGet packages and JSON.NET write in the search. Well, or take the DLL from the site and in the same place through Add Reference ... specify.
Something like this:

using Newtonsoft.Json.Linq;
 ...

    dynamic root = JObject.Parse(json);

    int id = root.data[0].account_id;

M
mars2003, 2014-03-09
@mars2003

If this is ASP MVC and you need to get the values ​​in the controller action, then it is worth declaring the parameters with the desired type in the function definition, everything will be parsed by itself. for example

public void SaveData(int account_id, string nickname)
{
    //process data
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question