我收到错误:
System.InvalidOperationException:不支持类型“VK.Response” 用于反序列化数组。
我看起来像JSON:
{ “响应”:[{ “ID”:269058571 “如first_name”: “姓名”, “姓氏”: “名字”, “photo_50”:“http://cs624717.vk.me/v624717571/21718 /X8.jpg“}]}
我的方法是:
def new
if (logged_in?)
flash[:danger] = "You're already logged in"
redirect_to root_url
end
@organization = Organization.new
@member = @organization.members.build
end
类:
private void getFriendInfo()
{
string method = "users.get";
string param = "user_ids=269058571&fields=photo_50";
string url = "https://api.vk.com/method/" + method + "?" + param + "&v=5.31&access_token=" + key + "";
WebClient client = new WebClient();
string json = client.DownloadString(url);
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
RootObject response = (RootObject)json_serializer.Deserialize(json, typeof(RootObject));
for (int counter = 0; counter < response.response.items.Count; counter++)
{
pictureBox1.Load(response.response.items[counter].photo_50);
}
}
答案 0 :(得分:1)
尝试声明你的&#34; RootObject&#34;像这样的课:
class RootObject
{
public List<Response> response { get; set; }
}
答案 1 :(得分:1)
您的对象模型没有与json对齐。 试试这个(我现在使用的是Newtonsoft.Json,它是现在的标准JSON lib,而不是使用w / .Net的那个)使用nuget包管理器来获取它... https://www.nuget.org/packages/newtonsoft.json/:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var json = "{\"response\":[{\"id\":269058571,\"first_name\":\"Name\",\"last_name\":\"LastName\",\"photo_50\":\"http://cs624717.vk.me/v624717571/21718/X8.jpg\"}]}";
var obj = JsonConvert.DeserializeObject<RootObject>(json);
foreach(var item in obj.response) {
Console.WriteLine(item.first_name);
}
}
}
class RootObject
{
public List<Item> response { get; set; }
}
class Item
{
public string first_name { get; set; }
public string last_name { get; set; }
public string domain { get; set; }
public string photo_50 { get; set; }
}