在C#中,我有5-6天,我想尝试使用api一个站点。 我已反序列化JSON,这是格式
[ { “uid”:1476402, “first_name”:“”, “last_name”:“”, “域名”:“sandrische”, “在线”:1, “user_id”:1476402 }, { “uid”:3813182, “first_name”:“”, “last_name”:“”, “domain”:“id3813182”, “在线”:0, “user_id”:3813182 }, { “uid”:12789624, “first_name”:“”, “last_name”:“”, “domain”:“id12789624”, “在线”:0, “user_id”:12789624 }]
有一个班级
public class vkResponse
{
[JsonProperty(PropertyName = "uid")]
public int Id { get; set; }
[JsonProperty(PropertyName = "first_name")]
public string FirstName { get; set; }
[JsonProperty(PropertyName = "last_name")]
public string LastName { get; set; }
[JsonProperty(PropertyName = "photo_50")]
public Uri PhotoUri { get; set; }
[JsonProperty(PropertyName = "online")]
[JsonConverter(typeof(BoolConverter))]
public bool IsOnline { get; set; }
[JsonProperty(PropertyName = "lists")]
public List<int> Lists { get; set; }
}
public class BoolConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((bool)value) ? 1 : 0);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return reader.Value.ToString() == "1";
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(bool);
}
}
我想得到身份
var req = new HttpRequest();
string resp = req.Get("https://api.vk.com/method/friends.get?user_ids=1&fields=domain&access_token=" + GetToken()).ToString();
JObject o = JObject.Parse(resp);
JArray array = (JArray)o["response"];
vkResponse v = JsonConvert.DeserializeObject<vkResponse>(array.First().ToString());
richTextBox1.Text = v.Id.ToString();
但我只获得第一个ID,如何获得所有ID?
我认为这个问题array.First().ToString()
?请帮忙或举个例子。
答案 0 :(得分:1)
var v = JsonConvert.DeserializeObject<IEnumerable<vkResponse>>(array.ToString());
var userids = v.Select(x=>x.id);
答案 1 :(得分:0)
您的回复是vkResponse
个类的数组,因此您可以将其反序列化为c# array:
vkResponse[] vkResponses = JsonConvert.DeserializeObject<vkResponse[]>(array.ToString());
获得数组后,您可以遍历并访问每个元素的ID。
请告诉我如何循环访问并访问每个元素的ID
好的,这是使用基本c#循环结构和数组的方法:
vkResponse[] vkResponses = JsonConvert.DeserializeObject<vkResponse[]>(array.ToString());
if (vkResponses == null)
throw new JsonException();
int [] ids = new int[vkResponses.Length];
for (int i = 0; i < vkResponses.Length; i++)
{
ids[i] = vkResponses[i].Id;
}
如果要在富文本框中将ID显示为以逗号分隔的整数序列,请使用以下方法生成字符串:
public static string ExtractVkResponseIds(string vkResponseJson)
{
vkResponse[] vkResponses = JsonConvert.DeserializeObject<vkResponse[]>(vkResponseJson);
if (vkResponses == null)
throw new JsonException();
StringBuilder sb = new StringBuilder();
// Format the ids as a comma separated string.
foreach (var response in vkResponses)
{
if (sb.Length > 0)
sb.Append(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator);
sb.Append(response.Id.ToString());
}
return sb.ToString();
}
并称之为:
var req = new HttpRequest();
string resp = req.Get("https://api.vk.com/method/friends.get?user_ids=1&fields=domain&access_token=" + GetToken()).ToString();
JObject o = JObject.Parse(resp);
JArray array = (JArray)o["response"];
string ids = ExtractVkResponseIds(array.ToString());
richTextBox1.Text = ids;
顺便说一下,我使用了本地化的ListSeparator,这可能不是您所用语言的逗号。如果需要,可以将其更改为文字逗号。
顺便说一句,您的示例Json字符串缺少结束括号(&#34;]&#34;)。