我目前正在编写代码,但是我收到错误"无法访问Newtonsoft.Json.Linq.JValue上的子值"当我试图从我的json获得一个关于心室"行"。
的值我将数据作为JObject并通过httplink收集,因此我可以很好地获取数据。但是我很难收集我选择的所选行之一。这是代码:
async void loadPhot ()
{
var getItems = await parse.getPhotos ();
foreach (var moreitems in getItems["results"])
{
var Pic = "";
Pic = (string)moreitems["Image"][0]; //error here. trying to get the first data in the json
picture.Source = Pic; //picture is my x:name of my image in xaml.
}
}
json看起来像这样:
{
"results": [
{
"Image": "url"
},
{
"Image": "url2"
}
]
}
答案 0 :(得分:1)
只需替换
Pic = (string)moreitems["Image"][0]; //error here. trying to get the first data in the json
带
Pic = (string)moreitems["Image"]; //error here. trying to get the first data in the json
要获取特定行的项目,请执行以下操作:
var rows = getItems["results"];
int desiredRow = 0;
var itemAtRow = rows.ElementAt(desiredRow);
var imageAtRow = itemAtRow["Image"];
答案 1 :(得分:0)
如果您总是想要第一个值,那么请不要使用循环
var getItems = await parse.getPhotos ();
// get the value of the first item in the results
var Pic = (string)getItems[0];
picture.Source = Pic; //picture is my x:name of my image in xaml.