如何在Xamarin.forms列表视图中显示JSON数据?
我不熟悉如何在Xamarin.forms列表视图中显示JSON数据?,json,xamarin.forms,cross-platform,hybrid-mobile-app,Json,Xamarin.forms,Cross Platform,Hybrid Mobile App,我不熟悉xamarin.forms,我想构建一个跨平台的应用程序,在列表视图中显示JSON数据。这是我的JSON数据 { "fish_product": [ { "fish_id": "1", "fish_img": "", "fish_name": "Indian Mackerel", "fish_category": "Marine Fish", "size": "Medium", "price": "100
xamarin.forms
,我想构建一个跨平台的应用程序,在列表视图中显示JSON数据。这是我的JSON数据
{
"fish_product": [
{
"fish_id": "1",
"fish_img": "",
"fish_name": "Indian Mackerel",
"fish_category": "Marine Fish",
"size": "Medium",
"price": "100"
},
{
"fish_id": "2",
"fish_img": "",
"fish_name": "Manthal Repti",
"fish_category": "Marine Fish",
"size": "Small",
"price": "200"
},
{
"fish_id": "4",
"fish_img": "",
"fish_name": "Baby Sole Fish",
"fish_category": "Marine Fish",
"size": "Small",
"price": "600"
}
]
}
我想在列表视图中显示“fish_name”。请提供解决方案、文章或教程。提前谢谢。我认为您必须将此JSON反序列化为如下对象
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public ObservableCollection<FishProduct> fish_product { get; set; }
}
我想你可以看看我想你必须将这个JSON反序列化为
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public ObservableCollection<FishProduct> fish_product { get; set; }
}
我想你可以看看我想这可能会对你有所帮助 你的模型
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public List<FishProduct> fish_product { get; set; }
}
公共类产品
{
公共字符串fish_id{get;set;}
公共字符串fish_img{get;set;}
公共字符串fish_name{get;set;}
公共字符串fish_类别{get;set;}
公共字符串大小{get;set;}
公共字符串price{get;set;}
}
公共类根对象
{
公共列表fish_乘积{get;set;}
}
Webservice调用和JSON反序列化
public async Task GetData()
{
try
{
HttpClient client = new HttpClient();
var result = await client.GetAsync("http://yourJSON_Url");
result.EnsureSuccessStatusCode();
string json = await result.Content.ReadAsStringAsync();
List<FishProduct> res= JsonConvert.DeserializeObject<List<FishProduct>>(json);
}
catch (Exception ex)
{
throw;
}
}
公共异步任务GetData()
{
尝试
{
HttpClient=新的HttpClient();
var result=await client.GetAsync(“http://yourJSON_Url");
result.EnsureSuccessStatusCode();
string json=wait result.Content.ReadAsStringAsync();
List res=JsonConvert.DeserializeObject(json);
}
捕获(例外情况除外)
{
投掷;
}
}
我想这可能会对你有所帮助 你的模型
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public List<FishProduct> fish_product { get; set; }
}
公共类产品
{
公共字符串fish_id{get;set;}
公共字符串fish_img{get;set;}
公共字符串fish_name{get;set;}
公共字符串fish_类别{get;set;}
公共字符串大小{get;set;}
公共字符串price{get;set;}
}
公共类根对象
{
公共列表fish_乘积{get;set;}
}
Webservice调用和JSON反序列化
public async Task GetData()
{
try
{
HttpClient client = new HttpClient();
var result = await client.GetAsync("http://yourJSON_Url");
result.EnsureSuccessStatusCode();
string json = await result.Content.ReadAsStringAsync();
List<FishProduct> res= JsonConvert.DeserializeObject<List<FishProduct>>(json);
}
catch (Exception ex)
{
throw;
}
}
公共异步任务GetData()
{
尝试
{
HttpClient=新的HttpClient();
var result=await client.GetAsync(“http://yourJSON_Url");
result.EnsureSuccessStatusCode();
string json=wait result.Content.ReadAsStringAsync();
List res=JsonConvert.DeserializeObject(json);
}
捕获(例外情况除外)
{
投掷;
}
}
先生,您能推荐异步调用示例吗?@Alessandro Caliaroasync Call的作用是什么?您的问题是“如何显示…”您不需要异步。您需要bindingsSir您能推荐异步调用示例吗?@Alessandro Caliaroasync调用什么?您的问题是“如何显示…”您不需要异步。您需要绑定成功地从API获得响应?没有,先生。实际上,我们必须显示这种类型的json数据。可以建议一些代码吗?@Alex Chengalan尝试一下:成功地从您的API获得响应吗?没有,先生。实际上,我们必须显示这种类型的json数据。可以建议一些代码吗?@Alex Chengalan尝试一下: