Answer the question
In order to leave comments, you need to log in
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
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; }
}
static void Main(string[] args)
{
string testJson = "{'name':'Test','response':1}";
dynamic resultDynamic = JObject.Parse(testJson);
Console.WriteLine($"Name: {resultDynamic.name}, Response: {resultDynamic.response}");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question