我正在尝试获取传入的 JSON 项目并将它们绑定到列表框项目,但 Visual Studio 告诉我我需要做一个数组而不是对象?我从来没有这样做过......有人知道怎么做吗?
我的根对象:
public class RootObject
{
public string url { get; set; }
public string display { get; set; }
public List<string> genetics { get; set; }
public List<string> price { get; set; }
public List<string> brandMaker { get; set; }
public string form { get; set; }
public string dosornos { get; set; }
public string qty { get; set; }
public string mfg { get; set; }
public string mobURI { get; set; }
}
注意:Genetics、Price、BrandMaker 实际上只返回一个值,如下所示:
"genetics": [
"typeophere"
],
"price": [
"$1400"
],
JSON 文件/请求基本结果:
[
{
"url": "N/A",
"display": "",
"genetics": [
"microogiz"
],
"price": [
"96.016"
],
"brandMaker": [
"Oshi Kunti Multikashi, Osaka, JP"
],
"form": "tangent",
"dosornos": "n/a",
"qty": "88G",
"mfg": "SelfMade Industries, USA Dist.",
"mobURI": "n/a"
}
]
我的原始代码:
// Get JSON via WEB
string ProviderURI = goURI;
webClient webClient = new WebClient();
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(ProviderURI));
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
var deserializedJSON = JsonConvert.DeserializeObject<RootObject>(e.Result);
lstBoxResults.ItemsSource = deserializedJSON; // or deserializedJSON.url.ToString();
}