Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Json列表填充TableView_C#_Arrays_List_Uitableview_Xamarin - Fatal编程技术网

C# 从Json列表填充TableView

C# 从Json列表填充TableView,c#,arrays,list,uitableview,xamarin,C#,Arrays,List,Uitableview,Xamarin,我正在为C中的iOS做一个Xamarin项目 我有一个Json文件,其中包含4项。我希望它们填充TableView,但只显示Json文件中的最后一项 以下是我从Json文件中获取列表的方式: StreamReader strm = new StreamReader (filePath); response = strm.ReadToEnd (); List<Field> items = JsonConvert.DeserializeObject<List<Field&g

我正在为C中的iOS做一个Xamarin项目

我有一个Json文件,其中包含4项。我希望它们填充TableView,但只显示Json文件中的最后一项

以下是我从Json文件中获取列表的方式:

StreamReader strm = new StreamReader (filePath);
response = strm.ReadToEnd ();

List<Field> items = JsonConvert.DeserializeObject<List<Field>>(response);
我试图像这样填充数组:

int count = items.Count;
string[] tableItems = new string[count];
foreach(Field item in items)
{
    tableItems = new string[] { item.Value };
}
table.Source = new TableSource(tableItems);
Add(table);
这是表源:

public TableSource (string[] items)
{
    TableItems = items;
}
这是可能的吗?我如何实现它,使我的Json项位于TableView中

编辑

这是Json文件。我只是在ViewController中解析了它,它工作了,但在TableViewController中没有

[{
'id': 0,
'type': 'textField',
'value': 'Vul hier je voornaam in.',
'extra': ''
},{
'id': 1,
'type': 'textField',
'value': 'Vul hier je achternaam in.',
'extra': ''
},{
'id': 2,
'type': 'textField',
'value': 'Vul je gebruikersnaam in.',
'extra': ''
},{
'id': 3,
'type': 'textField',
'value': 'Vul je wachtwoord in.',
'extra': 'password'
}]

我已经用本文中的示例修复了它:

我的foreach循环现在针对所有有相同问题的人如下所示:

foreach(Field item in items)
{
    tableItems[item.Id] = item.Value;
}

您可以利用诸如LINQ之类的C特性,使其成为一个更简单的过程:

        table.Source = new TableSource(
            JsonConvert.DeserializeObject<List<Field>> (response).
            Select (a => a.Value).
            ToArray ()
        );
当前代码的问题是,如果Id没有0索引和顺序,则会遇到异常。如果要对项目进行排序,则可以轻松添加排序:

        table.Source = new TableSource(
            JsonConvert.DeserializeObject<List<Field>> (response).
            OrderBy(s => s.Id).
            Select (a => a.Value).
            ToArray ()

你能发布你试图反序列化的JSON吗?我检查了foreach循环中tableItems的长度,它总是1。问题就在这里的某个地方。如果我需要从列表中选择多个值,并将其与Where函数相结合,该怎么办?它是一个可枚举的函数,所以您可以标记任何您想要的内容。是否希望源现在包含字段的集合?Hre是一个可能有用的链接: