【发布时间】:2020-10-22 01:29:27
【问题描述】:
我对 c# 有点陌生,我正在尝试添加从 Web 服务返回的所有记录,并将它们添加到指定的列表中。我的 JSON 看起来像这样:
{
"errMsg":"",
"date":"2020-10-21 10:20:28",
"Category":
[{"iCatID":"1","sDescription":"All},{"iCatID":"2","sDescription":"All Vegetables"},....],
"Product":
[{"iProductID":"10","sDescription":"Apples}, {"iProductID":"11","sDescription":"All Vegetables"},....]
}
产品和类别都返回了几个 100 或 1000 个产品。到目前为止,我有这个尝试遍历它们并将它们添加到自己的列表中,然后将这两个列表添加到我的应用程序中名为 Product 和 Category 的两个本地表中。
我显然做错了什么,因为我当前的代码不起作用。 私有列表_product = new List(); 私有列表_category = new List();
public async void CreateLocalTables()
{
try
{
_client.GetAsync("https://app.spcty.com/app/dbExport.php");
var content = await response.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(content);
foreach (var obj in data.Product)
{
Console.WriteLine(obj);
//does not work
_product.Add(obj)
}
}
我的错误如下 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.List
我检查了我的类别课程,我认为我拥有我需要的一切。
public class Category
{
[PrimaryKey]
public int iCatID { get; set; }
public string sDescription { get; set; }
}
我不明白它所说的无效参数。我得到这个类别和产品。有什么建议吗? 编辑:我也尝试过使用 RootObject,我只是不知道如何从那里访问产品和类别列表:
public class RootObject
{
public List<Category> CategoyItems { get; set; }
public List<Product> ProductItems { get; set; }
public string errMsg { get; set; }
public string date { get; set; }
}
我在 JSON 和 RootObject 中又添加了两个东西。
【问题讨论】:
-
你好,你能显示
response.Content.ReadAsStringAsync()的示例内容数据吗?我会检查数据的结构。 -
它很长,但是让我看看我能不能给你那个
-
@JuniorJiang-MSFT 我添加了更多我的 JSON,我还添加了一些东西,希望能给你一个更好的描述。
-
好的,感谢更新。如果你已经解决了这个问题,记得在有时间的时候标记答案。其他人知道解决方案会很有帮助。