S
S
Shato Daltsaev2015-04-01 15:04:56
In contact with
Shato Daltsaev, 2015-04-01 15:04:56

VK.API and C# how to handle response?

I am a green programmer to put it mildly. I started to pick VK api and a problem arose, for which I cannot find information.
How can I drive the response that I receive from VK into a string?

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int appId = 4851252;
            string email = "моя почта@yandex.ru"; 
            string password = "пароль"; 
            Settings settings = Settings.All;
            var api = new VkApi();
            api.Authorize(appId, email, password, settings);
            var group = api.Utils.ResolveScreenName("etorostov");
            long id = group.Id.Value;
            int totalCount;
            int count = 1;
            var wallpost = api.Wall.Get(id, out totalCount, count);
        }
    }
}

Everything is ok, as I see by debugging, I get the answer in JSON/. var wallpost - returns the class to me as I understand it? How can I take from the answer only what I need?
const string url = "https://api.vk.com/method/wall.get?owner_id=36578247&count=1&filter=all&v=5.9&access_token=6ac24ea999312c6d23da20290665d1d0ab4780fa50926570f3b55efc15218648bc1fd7707fbb3b0cf5392";
            const string json =
                @"{
                    'response': {
                      'count': 766,
                      'items': [
                        {
                          'id': 2107,
                          'from_id': 36578247,
                          'owner_id': 36578247,
                          'date': 1427839641,
                          'post_type': 'post',
                          'text': 'С повышением курса,цены на продукты первой необходимости значительно выросли. Не понятно почему и Российские производители так поднимают свои цены. Не ужели для того чтобы увеличить зарплату своих сотрудников? Я в этом сомневаюсь! Растет социальное неравенство! Все больше людей остается без работы! Да и что сейчас можно купить на прежнюю зарплату? Теперь не то что бы отдыхать, да и жить дорого!',
                          'post_source': {
                            'link': {
                              'url': 'http://kredit-otziv.ru/kurs-valyut/',
                              'title': 'Курс валют на завтра, сегодня, за неделю ЦБ РФ, Доллар, Евро и др.. | Кредит Отз...',
                              'description': ''
                            },
                            'type': 'widget',
                            'data': 'comments'
                          },
                          'comments': {
                            'count': 0,
                            'can_post': 0
                          },
                          'likes': {
                            'count': 0,
                            'user_likes': 0,
                            'can_like': 1,
                            'can_publish': 1
                          },
                          'reposts': {
                            'count': 0,
                            'user_reposted': 0
                          }
                        }
                      ]
                    }
                  }";

That's what I see in the debug, how can I put 'text' and 'title' into the string?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Goryunov, 2015-04-01
@Shatoidil

If I understand your question correctly, then you can use a library for working with JSON, for example json.codeplex.com .
In this case, the code will look like this

//в начале 
using Newtonsoft.Json.Linq;

//... где то в коде
const string json =
                @"{
                    'response': {
                      'count': 766,
                      'items': [
                        {
                          'id': 2107,
                          'from_id': 36578247,
                          'owner_id': 36578247,
                          'date': 1427839641,
                          'post_type': 'post',
                          'text': 'С повышением курса,цены на продукты первой необходимости значительно выросли. Не понятно почему и Российские производители так поднимают свои цены. Не ужели для того чтобы увеличить зарплату своих сотрудников? Я в этом сомневаюсь! Растет социальное неравенство! Все больше людей остается без работы! Да и что сейчас можно купить на прежнюю зарплату? Теперь не то что бы отдыхать, да и жить дорого!',
                          'post_source': {
                            'link': {
                              'url': 'http://kredit-otziv.ru/kurs-valyut/',
                              'title': 'Курс валют на завтра, сегодня, за неделю ЦБ РФ, Доллар, Евро и др.. | Кредит Отз...',
                              'description': ''
                            },
                            'type': 'widget',
                            'data': 'comments'
                          },
                          'comments': {
                            'count': 0,
                            'can_post': 0
                          },
                          'likes': {
                            'count': 0,
                            'user_likes': 0,
                            'can_like': 1,
                            'can_publish': 1
                          },
                          'reposts': {
                            'count': 0,
                            'user_reposted': 0
                          }
                        }
                      ]
                    }
                  }";

//... еще где-то в коде
var data = JObject.Parse(json);
var items = data["response"]["items"];

string text = items[0]["text"].Value<string>();
string name = items[0]["post_source"]["link"]["title"].Value<string>();

Something like this, more details here ;) www.newtonsoft.com/json/help/html/LINQtoJSON.htm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question