【发布时间】:2019-04-26 09:18:16
【问题描述】:
我有一个本地 JSON 文件,我需要在我的应用程序上显示它的一些数据,我尝试了一些解决方案但没有任何反应,我在另一个“Lighter”的 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>
作为结果,我希望显示纬度和经度,但我什么也没得到
【问题讨论】:
-
您将 ItemSource 设置为 GlcPart 列表,因此 xaml 需要 GlcPart 上的纬度和经度属性,而它们实际上位于嵌套的 Coordinate 类中。
-
@Metheny 当我将 ObjContactList 更改为 Coordinate Class 时,它稍后会在 l"istviewGLC.ItemsSource = ObjContactList...." 中给我一个错误,因为通常它需要一个 List ,因此可以访问列表,我使用了具有坐标列表的 GlcPart
标签: c# json xamarin xamarin.forms