显示我的应用程序上的JSON本地文件的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示我的应用程序上的JSON本地文件的数据相关的知识,希望对你有一定的参考价值。
我有一个本地JSON文件,我需要在我的应用程序上显示它的一些数据,我尝试了一些解决方案,但没有发生任何事情,我在另一个“更轻”的JSON文件上尝试了以下代码,它有效,但它不适用于我的主JSON文件。
这是我的JSON文件:
"latitude": 49.241524,
"longitude": 4.063723,
"glcParts": [
"id": 1,
"coordinates": [
"latitude": 49.241527,
"longitude": 4.063755
,
"latitude": 49.241545,
"longitude": 4.063865
,
"latitude": 49.241543,
"longitude": 4.063995
,
"latitude": 49.241537,
"longitude": 4.064188
]
,
"id": 2,
"coordinates": [
"latitude": 49.241555,
"longitude": 4.063413
,
"latitude": 49.241571,
"longitude": 4.063260
,
"latitude": 49.241589,
"longitude": 4.063032
,
"latitude": 49.241600,
"longitude": 4.062884
]
],
"gicParts": [
"detectionZoneId": 1,
"relevanceZoneId": 2,
"direction": 0,
"iviType": 0,
"roadSign": [
"countryCode": "FR",
"trafficSignPictogram": 2,
"pictogramCategoryCode":
"nature": 6,
"serialNumber": 61
],
"extraText": [
"language": 714,
"content": "Rétrécissement"
,
"language": 714,
"content": "voie"
]
,
"detectionZoneId": 1,
"relevanceZoneId": 2,
"direction": 0,
"iviType": 1,
"roadSign": [
"countryCode": "FR",
"trafficSignPictogram": 2,
"pictogramCategoryCode":
"nature": 5,
"serialNumber": 57
],
"extraText": [
"language": 714,
"content": "/!\\ 50 km/h"
]
],
"timeStamp": "2019-03-08T16:00:00+01:00",
"validFrom": "2019-03-08T16:00:00+01:00",
"validTo": "2019-03-08T18:00:00+01:00",
"countryCode": 714,
"providerIdentifier": 4201,
"status": 0
C#类:
using System;
using System.Collections.Generic;
namespace interface_test
public class Coordinate
public double latitude get; set;
public double longitude get; set;
public class GlcPart
public int id get; set;
public List<Coordinate> coordinates get; set;
public class PictogramCategoryCode
public int nature get; set;
public int serialNumber get; set;
public class RoadSign
public string countryCode get; set;
public int trafficSignPictogram get; set;
public PictogramCategoryCode pictogramCategoryCode get; set;
public class ExtraText
public int language get; set;
public string content get; set;
public class GicPart
public int detectionZoneId get; set;
public int relevanceZoneId get; set;
public int direction get; set;
public int iviType get; set;
public List<RoadSign> roadSign get; set;
public List<ExtraText> extraText get; set;
public class IVIv2
public double latitude get; set;
public double longitude get; set;
public List<GlcPart> glcParts get; set;
public List<GicPart> gicParts get; set;
public DateTime timeStamp get; set;
public DateTime validFrom get; set;
public DateTime validTo get; set;
public int countryCode get; set;
public int providerIdentifier get; set;
public int status get; set;
主类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using interface_test;
using Newtonsoft.Json;
using Xamarin.Forms;
namespace interface_test
public partial class IVI_Display : ContentPage
public IVI_Display()
InitializeComponent();
GetJsonData();
void GetJsonData()
string jsonFileName = "JsonIVI.json";
GlcPart ObjContactList = new GlcPart();
var assembly = typeof(IVI_Display).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"assembly.GetName().Name.jsonFileName");
using (var reader = new StreamReader(stream))
var jsonString = reader.ReadToEnd();
//Converting JSON Array Objects into generic list
ObjContactList = JsonConvert.DeserializeObject<GlcPart>(jsonString);
//Binding listview with json string
listviewGLC.ItemsSource = ObjContactList.coordinates;
XAML要显示:
<ListView x:Name="listviewGLC" Grid.Row="1" HorizontalOptions="FillAndExpand" Footer="" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="FillAndExpand" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Text="Binding latitude" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<Label Text="Binding longitude" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue" FontAttributes="Bold"/>
<BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="2" HorizontalOptions="FillAndExpand" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
结果,我希望显示纬度和经度,但我没有得到任何东西
根据你的json,你应该在IVIv2对象中反序列化它而不是在GlcPart中。
替换:ObjContactList = JsonConvert.DeserializeObject<GlcPart>(jsonString);
与:ObjContactList = JsonConvert.DeserializeObject<IVIv2>(jsonString);
那之后你的
listviewGLC.ItemsSource = ObjContactList.GlcPart[0];
使用当前的模型结构,您无法创建包含GlcPart列表中所有值的列表视图。
ObjContactList必须是xaml中bind的公共属性
我建议使用一个明确的代码将ViewModel添加到ContentPage类并使用Bindingcontext。
public partial class IVI_Display : ContentPage
public IVI_DisplayViewModel ViewModel get; set;
public IVI_Display()
ViewModel = new IVI_DisplayViewModel();
...
BindingContext = ViewModel;
public class IVI_DisplayViewModel
public object Coordinates get; set;
...
在Xaml中使用
<ListView x:Name="listviewGLC" ItemsSource="Binding Coordinates Grid.Row="1" HorizontalOptions="FillAndExpand" Footer="" HasUnevenRows="True">
始终是一个良好的实践类型清晰的代码。
以上是关于显示我的应用程序上的JSON本地文件的数据的主要内容,如果未能解决你的问题,请参考以下文章
仅使用 Angular 7 将 json 数据写入本地 JSON 文件
从本地 json 文件中获取数据并显示在 html asp.net mvc 中