Answer the question
In order to leave comments, you need to log in
c# language, json.net and vk.com api parsing?
The point here is this, I'm trying to parse the wall of the social network VKontakte.
I succeed, but not completely. I was able to get 1 message, but how to output all this in a loop, I'm lost. In addition, I want an option that is elegant and not expensive in terms of resources. I'm new to c#, green as hell. My first program.
The server api answers me with this answer.
{
"response": [
5,
{
"id": 235,
"from_id": 00000,
"to_id": 00000,
"date": 1365266153,
"text": "Наконец-то открыл стенку!))",
"comments": {
"count": 0
},
"likes": {
"count": 0
},
"reposts": {
"count": 0
}
},
{
"id": 234,
"from_id": 00000,
"to_id": 000000,
"date": 1365264936,
"text": "тест 4",
"comments": {
"count": 0
},
"likes": {
"count": 0
},
"reposts": {
"count": 0
}
},
{
"id": 233,
"from_id": 0000000,
"to_id": 0000000,
"date": 1365264931,
"text": "тест 3",
"comments": {
"count": 0
},
"likes": {
"count": 0
},
"reposts": {
"count": 0
}
},
{
"id": 232,
"from_id": 000000,
"to_id": 000000,
"date": 1365264926,
"text": "тест 2",
"comments": {
"count": 0
},
"likes": {
"count": 0
},
"reposts": {
"count": 0
}
},
{
"id": 231,
"from_id": 000000,
"to_id": 000000,
"date": 1365264922,
"text": "тест 1",
"comments": {
"count": 0
},
"likes": {
"count": 0
},
"reposts": {
"count": 0
}
}
]
}
JObject test = JObject.Parse(responseFromVk);
(string)test["response"][1].ToString();
vkClientMetro1.Wall getUser = JsonConvert.DeserializeObject<vkClientMetro1.Wall >((string)test["response"][1].ToString());
public class Wall
{
public int id { get; set; }
public int from_id { get; set; }
public int to_id { get; set; }
public int date { get; set; }
public string text { get; set; }
public Comments comments { get; set; }
public Likes likes { get; set; }
public Reposts reposts { get; set; }
}
Answer the question
In order to leave comments, you need to log in
I did not find clear examples for json and decided to use xml instead.
using (StringReader s = new StringReader(responseFromVk))
{
XDocument loadedData = XDocument.Load(s);
var data = from query in loadedData.Descendants("post")
select new vkClientMetro1.Wall
{
Id = (int)query.Element("id"),
From_id = (int)query.Element("from_id"),
To_id = (int)query.Element("to_id"),
Date = (int)query.Element("date"),
Text = (string)query.Element("text"),
};
listBox.ItemsSource = data;
}
Or maybe you shouldn’t reinvent your wheel, but should you take a ready-made one? — vk.codeplex.com/
Have you tried that? Then in the handicap do what you want with these messages.
List<Wall> messagesOnWall = JsonConvert.DeserializeObject<List<Wall>>(test["response"]);
Alternatively, you can use JavaScript for the WinRT project instead of C#. The code will come out as concise as possible:
var test = JSON.parse(responseFromVk);
var wall = test.response[1];
var temp = JsonConvert.DeserializeObject((string)test["response"][1].ToString()) as vkClientMetro1.Wall;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question