c# - 解析动态数据

标签 c# json dynamic

我正在尝试使用动态对象解析 JSON。这是我正在使用的库,即 JSON 数据转换器:https://github.com/cmcdonaldca/shopify.net

这是我的错误信息:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'string' does not contain a definition for 'products'

这是我的 JSON:

http://pastebin.com/pnmz7jN0

这是我的代码:

    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);
    }
}

Your sample data parsed

关于c# - 解析动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444473/

相关文章:

php - 通过PowerShell从JSON数组中提取特定对象作为逗号分隔的字符串

javascript - 使用 vanilla javascript 将 .json 数据添加到表中

go - Golang结构的动态属性设置

c# - StyleCop 自定义规则 : Method Parameters and Variables

json - Extjs Json加载没有Root

C# 使用 XmlText 序列化 XmlElement

Swift:我可以使用动态字体大小调整属性文本吗?

c# - FindControl 和动态创建的控件出现问题

c# - 如何根据默认样式制作圆边按钮样式

c# - 在 winmobile 中带有文本字段的自定义对话框