我有一個如下圖所示的 json:

我想將 json 放入一個串列視圖中(在串列視圖中有一個 webview 來顯示文本(我用藍色圈出),這是根據它的索引(我用黑色圈出)排序的)。
XAML:
<ListView
Name="ListPairOption"
Height="auto"
Margin="5,0,10,0">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:PairClass">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<StackPanel
x:Name="pilganStack"
Grid.Column="0"
Margin="10,10,10,10">
<WebView
x:Name="option"
MaxWidth="600"
MaxHeight="300"
Margin="5,5,5,5"
local:MyProperties.HtmlString="{Binding Name}"/>
</StackPanel>
</ListView
代碼:
string urlPath = "..../multiple/test/284/4";
var httpClient = new HttpClient(new HttpClientHandler());
httpClient.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", "token"));
var response = await httpClient.GetAsync(urlPath);
string jsonText = await response.Content.ReadAsStringAsync();
try
{
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonObject questionObject = jsonObject["EXAM_QUESTION"].GetObject();
int index = 0;
string c = "";
string v1 = "";
string choice = "";
ObservableCollection<PairClass> itemL = new ObservableCollection<PairClass>();
JsonArray mapArray = questionObject["map"].GetArray();
foreach (JsonValue mapValue in mapArray)
{
JsonArray mapArrayI = mapValue.GetArray();
foreach (JsonValue mapValueI in mapArrayI)
{
PairClass pair = new PairClass();
try
{
if (mapValueI.ToString().All(char.IsDigit))
{
c = String.Concat(mapValueI.ToString().Where(Char.IsDigit));
index = Int16.Parse(c);
pair.Ind = index;
}
else
{
string v = mapValueI.ToString();
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
v1 = item.ToString().Trim('"');
choice = v1;
pair.Name = choice;
}
}
for (int i = 0; i < varian; i )
{
itemL.Add(new PairClass { Ind = index, Name = choice });
}
itemL.Add(pair);
}
}
}
ListPairOption.ItemsSource = itemL;
}
對類:
public class PairClass
{
public int Ind { get; set; }
public string Name { get; set; }
public PairClass()
{
Ind = int.MinValue;
Name = string.Empty;
Pilihan = string.Empty;
}
public PairClass(int ind, string name)
{
Ind = ind;
Name = name;
}
}
我遇到了一個問題,無法將資料顯示到ListView中(listview中只重復顯示最后的資料),如下圖:

如何處理?
uj5u.com熱心網友回復:
我檢查了你的代碼,看起來你PairClass在錯誤的地方生成了它,它應該在決議地圖項之前創建,并在決議每個地圖項之后分配值。我已經更新了下面的代碼,請檢查它。
try
{
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonObject questionObject = jsonObject["EXAM_QUESTION"].GetObject();
int index = 0;
string c = "";
string v1 = "";
string choice = "";
ObservableCollection<PairClass> itemL = new ObservableCollection<PairClass>();
JsonArray mapArray = questionObject["map"].GetArray();
foreach (JsonValue mapValue in mapArray)
{
JsonArray mapArrayI = mapValue.GetArray();
PairClass pair = new PairClass();
foreach (JsonValue mapValueI in mapArrayI)
{
try
{
if (mapValueI.ToString().All(char.IsDigit))
{
c = String.Concat(mapValueI.ToString().Where(Char.IsDigit));
index = Int16.Parse(c);
pair.Ind = index;
}
else
{
string v = mapValueI.ToString();
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
v1 = item.ToString().Trim('"');
choice = v1;
pair.Name = choice;
}
}
}
catch
{
}
}
itemL.Add(pair);
}
ListPairOption.ItemsSource = itemL;
}
catch
{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367170.html
下一篇:限制惰性列中的專案
