6
6
6a6ajka2017-07-09 20:17:51
JSON
6a6ajka, 2017-07-09 20:17:51

JSON in c#, how to parse elements?

I'm learning this language for the first day, I thought everything would be as simple as in php, but it's not like that.
In general, for the test, I decided to try to make a simple bot.
But in the end it turned out that I can’t extract anything from the VK server response (json code).
How can I actually implement this?
As an example:
{"response": 1}
I googled a lot, but I didn't find the exact problem why it doesn't work for me.
The MB of the database did not download the same.
Suggest a working method.
Z.s. I had the 2010 version, download 2013.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Arxont, 2017-07-10
@6a6ajka

As already mentioned above - use the Newtonsoft.Json library.
Then there are two ways
(for example, I added the Name parameter and the test string looks like this -
1. Through the class. We create a class with a list of parameters and do

static void Main(string[] args)
        {
            string testJson = "{'name':'Test','response':1}";

            var result = JsonConvert.DeserializeObject<MyClass>(testJson);

            Console.WriteLine($"Name: {result.Name}, Response: {result.Response}");
        }

        class MyClass
        {
            public string Name { get; set; }
            public int Response { get; set; }
        }

2. There is another way using dynamic
static void Main(string[] args)
        {
            string testJson = "{'name':'Test','response':1}";

            dynamic resultDynamic = JObject.Parse(testJson);

            Console.WriteLine($"Name: {resultDynamic.name}, Response: {resultDynamic.response}");
        }

D
Dmitry Bashinsky, 2017-11-17
@BashkaMen

https://www.youtube.com/watch?v=NiZY7EmFSCE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question