如何解决JSON数据,xamarin形式简单展示
你好,我有一个很大的 JSON 文件,代表国家和他们的 Covid 总病例数和死亡人数。我想要做的就是将 JSON 的数据显示到我的 AboutPage 中。我尝试了各种方法,但没有人为我工作。知道我应该如何工作吗?
我给你一小部分 JSON
[{
"regionData": [
{
"country": "World","totalCases": 157341642,"newCases": 651328,"totalDeaths": 3278509,"newDeaths": 9126,"totalRecovered": 135480211,"activeCases": 18582922,"serIoUsCritical": 108879,"casesPerMil": 20185,"deathsPerMil": 420.6,"totalTests": 0,"testsPerMil": 0,"population": 0
},{
"country": "India","totalCases": 21886556,"newCases": 401271,"totalDeaths": 238265,"newDeaths": 4194,"totalRecovered": 17917013,"activeCases": 3731278,"serIoUsCritical": 8944,"casesPerMil": 15729,"deathsPerMil": 171,"totalTests": 298601699,"testsPerMil": 214596,"population": 1391457000
},]
}]
var assembly = typeof(AboutPage).GetTypeInfo().Assembly; Stream stream = assembly.GetManifestResourceStream("MaybeThisOne.testmodel.json");
using (var reader = new System.IO.StreamReader(stream))
{
var json = reader.ReadToEnd();
List<TestModel> mylist = JsonConvert.DeserializeObject<List<TestModel>>(json);
myrootobject = new ObservableCollection<TestModel>(mylist);
MyListView.ItemsSource = myrootobject;
JsonConvert.DeserializeObject
更新:
我执行了建议的步骤,1. 使用 json2csharp.com 创建 C# 模型,2. 使用 newtonsoft 反序列化。构建正在进行并且应用程序启动,但是当我尝试进入创建的页面时,它终止了应用程序,没有任何错误消息。
更新 2:
我开始了一个新项目并按照上述步骤操作。我得到的唯一错误是关于 Page3。我必须在 jsonarray 文件夹的 shell 中调用它。我该怎么做?
e.x <ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
请告诉我如何从 jsonarray 文件夹中调用 Page3?提前致谢!!!
更新 3
我从头开始了这个项目,并且它部分起作用了。唯一的问题是它没有显示所有的绑定字段。
有什么想法吗?
解决方法
我使用 Newtonsoft.Json 13.0.1 将 JSON 数组对象转换为通用列表,并且我还修改了您的 json.json 文件,删除了外括号。设置 json.json 文件 Build Action as Embedded resource
{
"regionData": [
{
"country": "World","totalCases": 157341642,"newCases": 651328,"totalDeaths": 3278509,"newDeaths": 9126,"totalRecovered": 135480211,"activeCases": 18582922,"seriousCritical": 108879,"casesPerMil": 20185,"deathsPerMil": 420.6,"totalTests": 0,"testsPerMil": 0,"population": 0
},{
"country": "India","totalCases": 21886556,"newCases": 401271,"totalDeaths": 238265,"newDeaths": 4194,"totalRecovered": 17917013,"activeCases": 3731278,"seriousCritical": 8944,"casesPerMil": 15729,"deathsPerMil": 171,"totalTests": 298601699,"testsPerMil": 214596,"population": 1391457000
}
]
}
有regionData属性包含列表数据,所以你需要创建新的类TestList,包含列表regionData属性
public class TestList
{
public List<TestModel> regionData { get; set; }
}
public class TestModel
{
public string country { get; set; }
public string totalCases { get; set; }
}
ContentPage.cs:
public Page3()
{
InitializeComponent();
TestList modellist = new TestList();
var assembly = typeof(jsonarray.Page3).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("FormsSample.json3.json");
using (var reader = new System.IO.StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
//Converting JSON Array Objects into generic list
modellist = JsonConvert.DeserializeObject<TestList>(jsonString);
listview1.ItemsSource = modellist.regionData;
}
}
终于有了ListView中的列表数据,没问题了。
更新:
我的项目名称是FormsSample,我在根目录添加了Json3.json文件,Build Action as Embedded resource,可以看到如下截图。
Page 3 是一个 ContentPage,包含 ListView,用于显示 Json 数据。
<ContentPage
x:Class="FormsSample.jsonarray.Page3"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="listview1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding country}" />
<Label
HorizontalOptions="CenterAndExpand"
Text="{Binding totalCases}"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。