解析 Xamarin 表单中的嵌套 JSon 文件

爱莎

我正在使用 Xamarin 开发跨平台应用程序。数据源是一个 JSON 文件,其中包含一些单词及其押韵,如下所示。我必须从 JSON 文件中读取单词,为此我设计了一个模型类“WordRhyme.cs”。输出. 我的代码工作正常,但由于某些原因,我无法检索每个单词的押韵。过去三天我一直试图找到解决方案,但没有运气。当 Rhymes 返回一个列表时,我还尝试用嵌套在单词列表中的列表替换标签,但这不起作用(下面添加了 xaml 编码)。还有一种方法可以只从 JSON 中选择几条记录,因为它可能有数百条记录,但我想显示一些。我之前没有在 Xamarin 中编程,所以谷歌搜索学习。

json1.json

{
  "words": [

    {
      "word": "coffee",
      "syllables": 2,
      "topic": "Food and drink",
      "rhymes": [
        {
          "word": "toffee",
          "score": 385,
          "numSyllables": 2
        },
        {
          "word": "naafi",
          "score": 178,
          "numSyllables": 2
        },
        {
          "word": "tophi",
          "score": 172,
          "numSyllables": 2
        },
        {
          "word": "sofie",
          "score": 75,
          "numSyllables": 2
        },
          {
          "word": "yoffie",
          "numSyllables": 2
        }
      ]
    },

    {
      "word": "Cat",
      "syllables": 2,
      "topic": "Animals",
      "rhymes": [
        {
          "word": "Hat",
          "score": 385,
          "numSyllables": 2
        },
        {
          "word": "Fat",
          "score": 178,
          "numSyllables": 2
        },
        {
          "word": "Mat",
          "score": 172,
          "numSyllables": 2
        },
        {
          "word": "coffey2",
          "score": 152,
          "numSyllables": 2
        },
        {
          "word": "toffy2",
          "score": 119,
          "numSyllables": 2
        }      ]
    }
  ]
}

字韵.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace JsonTest2.Models
{

    public class Rhyme
    {
        public string word { get; set; }
        public int score { get; set; }
        public int numSyllables { get; set; }
    }

    public class Word
    {
        public string word { get; set; }
        public int syllables { get; set; }
        public string topic { get; set; }
        public List<Rhyme> rhymes { get; set; }
    }

    public class WordRhymes
    {
        public List<Word> words { get; set; }
    }
}

页面1.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="JsonTest2.Page1">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <Grid>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Margin="30" Text="Rhyming Words JSON Parsing" FontSize="25" />
                    <ListView x:Name="listViewWords" 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"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Label Text="{Binding word}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue"  FontAttributes="Bold"/>
                                        <Label Text="{Binding syllables}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange"  FontAttributes="Bold"/>
                                        <Label Text="{Binding topic}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray"  FontAttributes="Bold"/>
                                        <Label Text="{Binding rhymes.word}" Grid.Row="3" TextColor="Blue"  FontAttributes="Bold"/>
                                        <Label Text="{Binding rhymes.score}" Grid.Row="4" TextColor="Blue"  FontAttributes="Bold"/>
                                        <Label Text="{Binding rhymes.numSyllables}" Grid.Row="5" TextColor="Blue"  FontAttributes="Bold"/>
                                        <BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="6" HorizontalOptions="FillAndExpand" />
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

页面1.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JsonTest2.Models;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace JsonTest2
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
            GetJsonData();
        }

        void GetJsonData()
        {
            string jsonFileName = "json1.json";
            WordRhymes ObjWordList = new WordRhymes();
            var assembly = typeof(Page1).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
            using (var reader = new System.IO.StreamReader(stream))
            {
                var jsonString = reader.ReadToEnd();

                //Converting JSON Array Objects into generic list  
                ObjWordList = JsonConvert.DeserializeObject<WordRhymes>(jsonString);
            }
            //Binding listview with json string   
            listViewWords.ItemsSource = ObjWordList.words;
        }
    }
}

嵌套 ListView Page2Json.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="JsonTest2.Page2Json">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <Grid>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Margin="30" Text="Rhyming Words JSON Parsing" FontSize="25" />
                    <ListView x:Name="listViewWords" 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"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Label Text="{Binding word}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="Blue"  FontAttributes="Bold"/>
                                        <Label Text="{Binding syllables}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange"  FontAttributes="Bold"/>
                                        <Label Text="{Binding topic}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray"  FontAttributes="Bold"/>
                                        <ListView x:Name="listViewRhymes" HorizontalOptions="FillAndExpand" ItemsSource="{Binding listViewWords.rhymes}">
                                            <ListView.ItemTemplate>
                                                <DataTemplate>
                                                    <ViewCell>
                                                        <Grid HorizontalOptions="FillAndExpand">
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto"/>
                                                                <RowDefinition Height="Auto"/>
                                                                <RowDefinition Height="Auto"/>
                                                            </Grid.RowDefinitions>
                                                            <Label Text="{Binding rhymes.word}"></Label>
                                                            <Label Text="{Binding rhymes.score}"></Label>
                                                            <Label Text="{Binding rhymes.numSyllables}"></Label>
                                                        </Grid>
                                                    </ViewCell>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>

                                        </ListView>
                                        <Label Text="{Binding rhymes.word}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray"  FontAttributes="Bold"/>
                                        <BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="3" HorizontalOptions="FillAndExpand" />
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                    <StackLayout Orientation="Horizontal">
                        <Button x:Name="nextButton" Text="Next" Clicked="NextButton_Clicked"></Button>
                        <Button x:Name="backButton" Text="Back" Clicked="BackButton_Clicked"></Button>
                    </StackLayout>
                </Grid>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
阿拜·加格

所以在 Page2Json.xaml 的内部列表中,你的 itemsource 绑定了listViewWords.rhymes我认为应该只绑定的,rhymes即使在这个列表视图标签内也不应该绑定,rhymes.something而它们应该在something这里是我认为应该工作的更新代码

<ListView x:Name="listViewRhymes" HorizontalOptions="FillAndExpand" ItemsSource="{Binding rhymes}">
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <ViewCell>
                                                    <Grid HorizontalOptions="FillAndExpand">
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition Height="Auto"/>
                                                            <RowDefinition Height="Auto"/>
                                                            <RowDefinition Height="Auto"/>
                                                        </Grid.RowDefinitions>
                                                        <Label Text="{Binding word}"></Label>
                                                        <Label Text="{Binding score}"></Label>
                                                        <Label Text="{Binding numSyllables}"></Label>
                                                    </Grid>
                                                </ViewCell>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>

                                    </ListView>

这些更改考虑到您的 page2json.xaml.cs 与 page1json.xaml.cs 相同

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章