Z
Z
zepars2015-07-12 17:48:38
JSON
zepars, 2015-07-12 17:48:38

How to create a JSON pattern if the parameter name is unknown?

Vkontakte API gives the following JSON result:

{
"response": [
2,
{
"gid": 20629724,
"name": "Habrahabr",
"screen_name": "habr",
"is_closed": 0,
"type": "page",
"is_admin": 0 ,
"is_member": 1,
"photo": "http:\/\/cs625620.vk.me\/v625620167\/2ac9d\/QholHa-tKsI.jpg",
"photo_medium": "http:\/\/cs625620 .vk.me\/v625620167\/2ac9c\/aqvP2mwKWvQ.jpg",
"photo_big": "http:\/\/cs625620.vk.me\/v625620167\/2ac9b\/F1E7HSFXLr0.jpg"
},
{
"gid ": 69578162,
"name": "Habrakon",
"screen_name": "horse_habr",
"is_closed": 0,
"type": "page",
"is_admin": 0,
"is_member": 0,
"photo": "http:\/\/cs606920.vk.me\/v606920089\/500b\/HMVpfZKx_uQ.jpg",
"photo_medium": "http:\/ \/cs606920.vk.me\/v606920089\/500a\/2R8ePCnuhiw.jpg",
"photo_big": "http:\/\/cs606920.vk.me\/v606920089\/5009\/-pPfLgf2oB4.jpg"
}
]
}

I need to deserialize JSON to List. I do like this:
JavaScriptSerializer().Deserialize<List<VKPatternGroups>>(JSON); 
 public class VKPatternGroups
            {
                public List<VKPatternGroup> response { get; set; } //IList<VKPatternGroup>
            }

            public class VKPatternGroup
            {
                public string gid { get; set; }
                public string name { get; set; }
                public string type { get; set; }
               // public string members_count { get; set; }
               
            }

But the number of elements in the sheet: 0. There are many examples on the Internet, but there is a name of the parameters, and here, as far as I understand, only the values ​​of the response. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Gromadchuk, 2015-07-12
@zepars

Use latest api version not 3.0. Then there will be no such misunderstandings.
v=5.34
https://vk.com/dev/versions

T
Tsiren Naimanov, 2015-07-12
@ImmortalCAT

public class VKGroupObject
    {
        public ResponseGroup response { get; set; }
    }

    public class ResponseGroup
    {
        public string count { get; set; }
        public IList<VKGroupItems> items { get; set; }
    }
    public class VKGroupItems
    {
        public string id { get; set; }
        public string name { get; set; }
        public string screen_name { get; set; }
        public string is_admin { get; set; }
        public string admin_level { get; set; }
        public string photo_50 { get; set; }
    }

This is my class
public List<T> GetData<T>(string json)
        {
            List<T> group = new List<T>();
            group.Add(JsonConvert.DeserializeObject<T>(json));
            return group;
        }

Here is the deserialization method
PS Connect and use Newtonsoft.Json
-> Install-Package Newtonsoft.Json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question