gpt4 book ai didi

C# JSON 错误(将值 "username"转换为类型时出错)

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:38 24 4
gpt4 key购买 nike

我收到 JSON 回复 instagram 和“序列化部分 JSON 片段”。

它是 JSON 回复

{
"access_token": "Here token",
"user": {
"username": "here user name",
"bio": "her bio.",
"website": "her website",
"profile_picture": "http://images.ak.instagram.com/profiles/here picture",
"full_name": "her name",
"id": "and her id"
}
}

我创建了一个用户类:

public class User
{
public string id { get; set; }
public string username { get; set; }
public string full_name { get; set; }
public string profile_picture { get; set; }
}

并序列化

JObject instaCall = JObject.Parse(responseString);
IList<JToken> results = instaCall["user"].Children().ToList();
foreach (JToken apiResult in results)
{
User searchResult = JsonConvert.DeserializeObject<User>(apiResult.ToString());
}

并给我一个错误:

Error converting value "username" to type 'WpfApplication1.Control.BasicPage1+User'. Path '', line 1, position 10.

最佳答案

正如其他人所说,您无需循环访问要反序列化的 json。另一种仅从 json 转换用户对象的方法:

JObject instaCall = JObject.Parse(responseString);    
User searchResult = instaCall["user"].ToObject<User>();

关于C# JSON 错误(将值 "username"转换为类型时出错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com