我这里有这样的代码(对API的请求)
private void button1_Click(object sender, EventArgs e)
{
var dummy = new HttpRequest();
try
{
var response = dummy.Get(
"https://api.vk.com/method/method_name?parameters&access_token").ToString();
richTextBox1.Text = response;
}
catch
{
richTextBox2.Text = "Error!";
}
}
回应是关于这样一个json:
{"response":{"count":1318,"items":[{"id":7605931,"first_name":"Catarina","last_name":"Fox"},{"id":88593908,"first_name":"Анжелика","last_name":"Мартиросян"},{"id":10902759,"first_name":"Aneli","last_name":"Noita"},{"id":125376088,"first_name":"Саша","last_name":"Райнес"},{"id":112363011,"first_name":"Анастасия","last_name":"Махмудова"}]}}
Converter已发布以下类:
public class Item
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
}
public class Response
{
public int count { get; set; }
public List<Item> items { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
如何正确应用可用数据将json转换为数组?
答案 0 :(得分:1)
您可以通过JSON.net库(通过Nuget安装)
来完成此操作RootObject bsObj = JsonConvert.DeserializeObject<RootObject>(response);
或者您可以通过JavaScriptSerializer
执行此操作using System.Web.Script.Serialization;
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(response);