P
P
p4p2013-04-07 00:02:34
JSON
p4p, 2013-04-07 00:02:34

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
            }
        }
    ]
}

Here is the response array whose element 0 contains the number of messages, and all other elements are the messages themselves, respectively.
JObject test = JObject.Parse(responseFromVk);
This is how I can get to the message: Then I can parse it into a c# object:
(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; }
    }

well, get getUser.text but, as I said at the beginning, only one thing, how to pick everything up and make it beautiful?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
p4p, 2013-04-07
@p4p

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;
                }

A
Alexander, 2013-04-07
@alexpp

Or maybe you shouldn’t reinvent your wheel, but should you take a ready-made one? — vk.codeplex.com/

F
FanKiLL, 2013-04-07
@FanKiLL

Have you tried that? Then in the handicap do what you want with these messages.

List<Wall> messagesOnWall = JsonConvert.DeserializeObject<List<Wall>>(test["response"]);

D
deleted-Brazier, 2013-04-08
@deleted-Brazier

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];

E
erick_voodoo, 2015-03-28
@erick_voodoo

var temp = JsonConvert.DeserializeObject((string)test["response"][1].ToString()) as vkClientMetro1.Wall;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question