我正在尝试使用动态对象解析 JSON。这是我正在使用的库,即 JSON 数据转换器:https://github.com/cmcdonaldca/shopify.net
这是我的错误信息:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'string' does not contain a definition for 'products'
这是我的 JSON:
这是我的代码:
public void GetProducts()
{
// The JSON Data Translator will automatically decode the JSON for you
dynamic data = _api.Get("/admin/products.json");
// the dynamic object will have all the fields just like in the API Docs
foreach (var product in data.products)
{
Console.Write(product.title);
}
}
我试过data.products和data,但我无法获取其中的product对象。
最佳答案
开始考虑使用 data[0].title
,但在 Debug模式下,您应该能够浏览对象,或使用即时窗口并运行一些手动查询。
关于错误,您的动态数据
可能只是一个字符串对象,尝试您自己的代码只是删除单词“products”。
public void GetProducts()
{
// The JSON Data Translator will automatically decode the JSON for you
dynamic data = _api.Get("/admin/products.json");
// the dynamic object will have all the fields just like in the API Docs
foreach (var product in data) //here is the change from the code you posted
{
Console.Write(product.title);
}
}
关于c# - 解析动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444473/