在我的应用程序上显示JSON本地文件中的数据

时间:2019-04-26 09:18:16

标签: c# json xamarin xamarin.forms

我有一个本地JSON文件,我需要在我的应用程序上显示其某些数据,我尝试了一些解决方案,但没有任何反应,我在另一个JSON文件“ Lighter”上尝试了以下代码,并且可以正常工作,但不适用于我的主要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>  

作为结果,我希望显示纬度和经度,但我什么也没得到

2 个答案:

答案 0 :(得分:0)

根据您的json,您应该在IVIv2对象而非GlcPart中反序列化它。

替换:ObjContactList = JsonConvert.DeserializeObject<GlcPart>(jsonString);

与:ObjContactList = JsonConvert.DeserializeObject<IVIv2>(jsonString);

所以之后

listviewGLC.ItemsSource = ObjContactList.GlcPart[0];

使用当前模型结构,您将无法创建包含GlcPart列表中所有值的列表视图。

答案 1 :(得分:0)

ObjContactList必须是在xaml中绑定的公共属性

我建议使用清晰的代码将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中使用

   public IVI_Display()
   {
        ViewModel = new IVI_DisplayViewModel();
        ...
        BindingContext = ViewModel;
   }    

总是好的做法是输入清晰的代码。

相关问题