我如何在 Xamarin 中读取两个同名的 json 数据
Posted
技术标签:
【中文标题】我如何在 Xamarin 中读取两个同名的 json 数据【英文标题】:How can i read two same name json data in Xamarin 【发布时间】:2021-05-31 12:33:15 【问题描述】:伙计们,它是我的 json 文件,但你可以看到它有多个名称 - ID 或类似的东西。我在 Xamarin 中有 2 页。在第一页中,我需要从 json 文件的第一部分获取数据。 “ID”-“名称”-“条形码”.... 等等。但是在第二页我需要从 Json 文件中的 CampaignList 中获取数据。但在活动列表部分也有“名称”-“ID”。因此,当我尝试从 CampaignList 程序中获取数据时,会为我提供来自 Root 类的数据。但我需要从 CampaignList 中获取。如何从 CampaignList 获取数据? 我的 Json 文件:
"ID": 370,
"Name": "NIVEA Sun F50 Kids Sprey Hassas 200ml",
"Barcode": "4005900253330",
"ClassID": 0,
"PriceSales": 139.13,
"PriceSalesWithTax": 164.1734,
"PriceList": 205.26,
"PriceListWithTax": 242.2068,
"PricePurchase": 139.13,
"PricePurchaseWithTax": 164.1734,
"DiscountSales": 0.0,
"DiscountOfficial": 0.0,
"DiscountAdvance": 0.0,
"DiscountReturn": 0.0,
"DiscountManual": 0.0,
"TaxRate": 18,
"MonthlyPurchaseLimit": 0,
"UnitCost": 0.0,
"TermDay": 0,
"Increase": 0.0,
"WebPriceSales": 0.0,
"_PriceSales": 0.0,
"ProductCompanyID": 0,
"Company": "NIVEA BIERSDORF",
"FollowingAmount": 0,
"CampaignList": [
"ID": 20458,
"Name": "1A NIVEA KAMPANYALARI",
"StartDate": "2020-04-13T00:00:00",
"EndDate": "2020-12-31T00:00:00",
"Amount": 1,
"ExtraAmount": 0,
"MinAmount": 0,
"Discount1": 10.00,
"Discount2": 0.00,
"Discount3": 0.00,
"Discount4": 0.00,
"TermDay": 0,
"CompanyCampaignDetailsID": 0,
"ProductID": 0,
"CampaignRate": 0.0,
"UnitPriceWithDiscountWtihTax": 0.0,
"UnitPriceAfterExtraAmount": 0.0,
"UnitPriceWithDiscount": 0.0,
"RowSummaryWithoutDiscount": 0.0,
"RowSummary": 0.0,
"DiscountTotal": 0.0,
"RowSummaryWithTax": 0.0,
"RowSummaryTaxAmount": 0.0,
"RowSummaryPriceSales": 0.0,
"UnitPriceAfterDiscount": 0.0
它在 c# 中
public class CampaignList
public int ID get; set;
public string Name get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;
public int Amount get; set;
public int ExtraAmount get; set;
public int MinAmount get; set;
public double Discount1 get; set;
public double Discount2 get; set;
public double Discount3 get; set;
public double Discount4 get; set;
public class Root
public int ID get; set;
public string Name get; set;
public string Barcode get; set;
public int ClassID get; set;
public double PriceSales get; set;
public double PriceSalesWithTax get; set;
public double PriceList get; set;
public double PriceListWithTax get; set;
public double PricePurchase get; set;
public double PricePurchaseWithTax get; set;
public double DiscountSales get; set;
public double DiscountOfficial get; set;
public double DiscountAdvance get; set;
public double DiscountReturn get; set;
public double DiscountManual get; set;
public int TaxRate get; set;
public int MonthlyPurchaseLimit get; set;
public double UnitCost get; set;
public int TermDay get; set;
public double Increase get; set;
public double WebPriceSales get; set;
public double _PriceSales get; set;
public int ProductCompanyID get; set;
public string Company get; set;
public int FollowingAmount get; set;
public List<CampaignList> CampaignList get; set;
public List<ImageList> ImageList get; set;
public int TotalQuantity get; set;
public double TotalAmount get; set;
public bool IsCampaign get; set;
我在 Xamarin 中有 2 页。在第一页中,它应该显示来自 Roots 的 json 文件的第一部分,例如名称:NIVEA Sun F50 Kids Sprey Hassas 200ml 但在第二页中,它应该显示来自 CampaignList 的 json 文件的第二部分,例如:名称:1A NIVEA KAMPANYALARI 所以它是我的代码页: 第一页(它的作品。它没有任何问题):
public async void GetJason()
HttpClient httpClient = new HttpClient();
var url = "http://api.lokmanecza.com/api/products/4005900253330";
var response = await httpClient.GetStringAsync(url);
Root myProducts = JsonConvert.DeserializeObject<Root>(response);
urunadi.Text = myProducts.Name;
kdv.Text = "%" + myProducts.TaxRate.ToString();
firma.Text = myProducts.Company;
dsf.Text = myProducts.PriceSalesWithTax.ToString("0.00");
psf.Text = myProducts.PriceListWithTax.ToString("0.00");
barkod.Text = myProducts.Barcode;
第二页:
public async void GetJason()
HttpClient httpClient = new HttpClient();
var url = "http://api.lokmanecza.com/api/products/4005900253330";
var response2 = await httpClient.GetStringAsync(url);
CampaignList myCampaignList = JsonConvert.DeserializeObject<CampaignList>(response2);
CampName.Text = myCampaignList.ID.ToString();
Miktar.Text = myCampaignList.Amount.ToString();
MF.Text = myCampaignList.ExtraAmount.ToString();
Oran.Text = "-----";
Minmiktar.Text = myCampaignList.MinAmount.ToString();
isk1.Text = myCampaignList.Discount1.ToString("0:00");
isk2.Text = myCampaignList.Discount2.ToString("0:00");
isk3.Text = myCampaignList.Discount3.ToString("0:00");
isk4.Text = myCampaignList.Discount4.ToString("0:00");
【问题讨论】:
请不要将代码或错误或数据(如 json)作为图像发布。请花时间将其作为文本发布并正确格式化以使其可读。 好的。谢谢你。我现在编辑它。 @Cfun 对不起兄弟,我再次编辑我的问题。我尽量说得好。但我猜我的英语还不够。对不起。 【参考方案1】:两个页面都将相同的数据反序列化为两个完全不同的模型。这行不通。第二页需要CampaignList
数据,它是第一页上Root
对象的数据的子元素。无需两次请求相同的数据
你可以做这样的事情
// pass the myProducts object from page1 to page2 on the constructor
// instead of calling the service again
CampaignList myCampaignList = myProducts.CampaignList;
// myCampaignList is a list of objects, you have to specify which one to use
CampName.Text = myCampaignList[0].ID.ToString();
Miktar.Text = myCampaignList[0].Amount.ToString();
MF.Text = myCampaignList[0].ExtraAmount.ToString();
这也将帮助很多人学会使用数据绑定而不是手动设置所有 UI 元素的值
【讨论】:
兄弟你太棒了,非常感谢你以上是关于我如何在 Xamarin 中读取两个同名的 json 数据的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Xamarin Forms 从 HealthKit 读取 ECG 数据