我正在尝试学习Blazor,C#。我创建了一个网站,该网站调用第三方API并将一些信息返回到网页。它显示一切正常,期望我班上有一项。该项目是一个包含大量信息的对象。
这是班上
namespace learnc.Models
{
public class BritishAirwaysModel
{
public Getba_Locationsresponse GetBA_LocationsResponse { get; set; }
}
public class Getba_Locationsresponse
{
public Country[] Country { get; set; }
}
public class Country
{
public string CountryName { get; set; }
public string CountryCode { get; set; }
public object City { get; set; }
}
}
在Razor组件中,我有以下代码
<tbody>
@foreach (var ba in balocations.GetBA_LocationsResponse.Country)
{
<tr>
<td>@ba.CountryName.</td>
<td>@ba.CountryCode</td>
<td>@ba.City</td>
<td>
</td>
</tr>
}
</tbody>
</table>
当它在屏幕上呈现时,它会在City中得到
{
"GetBA_LocationsResponse": {
"Country": [
{
"CountryName": "United Arab Emirates",
"CountryCode": "AE",
"City": [
{
"CityName": "Abu Dhabi",
"CityCode": "AUH",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Abu Dhabi",
"AirportCode": "AUH",
"Position": {
"Latitude": 24.43,
"Longitude": 54.65
}
}
},
{
"CityName": "Dubai",
"CityCode": "DXB",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Dubai",
"AirportCode": "DXB",
"Position": {
"Latitude": 25.25,
"Longitude": 55.36
}
}
}
]
},
{
"CountryName": "Antigua",
"CountryCode": "AG",
"City": {
"CityName": "Antigua",
"CityCode": "ANU",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Antigua",
"AirportCode": "ANU",
"Position": {
"Latitude": 17.14,
"Longitude": -61.79
}
}
}
},
{
"CountryName": "Albania",
"CountryCode": "AL",
"City": {
"CityName": "Tirana",
"CityCode": "TIA",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Tirana",
"AirportCode": "TIA",
"Position": {
"Latitude": 41.41,
"Longitude": 19.72
}
}
}
},
{
"CountryName": "Argentina",
"CountryCode": "AR",
"City": {
"CityName": "Buenos Aires",
"CityCode": "BUE",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Buenos Aires",
"AirportCode": "EZE",
"Position": {
"Latitude": -34.82,
"Longitude": -58.54
}
}
}
},
{
"CountryName": "Austria",
"CountryCode": "AT",
"City": [
{
"CityName": "Innsbruck",
"CityCode": "INN",
"Position": {
"Latitude": 0,
"Longitude": 0
},
"Airport": {
"AirportName": "Kranebitten",
"AirportCode": "INN",
"Position": {
"Latitude": 47.26,
"Longitude": 11.34
}
}
}
]
}
]
}
}
该怎么办,我只可以显示City对象中的几个项目。例如,假设我只想显示CityName和CityCode。 我为每个City对象编写了哪些代码来循环,然后仅从CityName和CityCode中拉出代码。
谢谢 罗素
答案 0 :(得分:4)
使用更好的命名方式会有所帮助。
@foreach (var ba in balocations.GetBA_LocationsResponse.Country)
读起来很奇怪,“国家/地区”是一个单数词,但显然这里是一个列表。尝试重命名事物,以便获得:
@foreach (var country in balocations.GetBA_LocationsResponse.Countries)
城市也是一样。在JSon中,它是一个数组,因此将其命名为Cities
。
然后很明显,您可以选择:Country
(ba)有很多Cities
,您想显示什么?
一个简单的建议,这样您至少可以看到以下内容:
@foreach (var ba in balocations.GetBA_LocationsResponse.Country)
{
<tr>
<td>@ba.CountryName.</td>
<td>@ba.CountryCode</td>
<td>@ba.City.FirstOrDefault()?.Name</td>
<td>
</td>
</tr>
}
好的,您发布了一些Json。它是不规则的,有些国家有1
"City": { ... },
而其他人有一个列表:
"City": [{ ... },{ ... } ],
这会混淆代码生成器。
最简单的方法是将响应复制/粘贴到临时文件,将其减少到仅1个国家(> = 2个城市)。确保Json有效,然后再次生成C#类。
还有许多在线Json工具站点可以帮助您分析,验证和转换Json。