【问题标题】:Access Json Structure in VB.Net在 VB.Net 中访问 Json 结构
【发布时间】:2015-03-24 13:13:39
【问题描述】:

我刚刚开始在我的程序中解析 Json 结构。 我的 ServerResponse 如下所示。

我不知道如何在此处粘贴 json 结构。因此,我只是在此处复制并粘贴结构。

{
"pot_searches": [
    {
        "id": 24,
        "links": {
            "results": [
                {
                    "type": "pot_search_results",
                    "id": 191
                },
                {
                    "type": "pot_search_results",
                    "id": 192
                },
                {
                    "type": "pot_search_results",
                    "id": 193
                },
                {
                    "type": "pot_search_results",
                    "id": 194
                },
                {
                    "type": "pot_search_results",
                    "id": 195
                },
                {
                    "type": "pot_search_results",
                    "id": 196
                },
                {
                    "type": "pot_search_results",
                    "id": 197
                },
                {
                    "type": "pot_search_results",
                    "id": 198
                },
                {
                    "type": "pot_search_results",
                    "id": 199
                },
                {
                    "type": "pot_search_results",
                    "id": 200
                }
            ],
            "query_tree": null
        },
        "description": "hello example",
        "query_text": "Heat",
        "limit": 10,
        "error": null,
        "created_by": null,
        "href": "http://blabla/bla/bla/pot/searches/24"
    },
    {
        "id": 25,
        "links": {
            "results": [
                {
                    "type": "pot_search_results",
                    "id": 201
                },
                {
                    "type": "pot_search_results",
                    "id": 202
                },
                {
                    "type": "pot_search_results",
                    "id": 203
                },
                {
                    "type": "pot_search_results",
                    "id": 204
                },
                {
                    "type": "pot_search_results",
                    "id": 205
                },
                {
                    "type": "pot_search_results",
                    "id": 206
                },
                {
                    "type": "pot_search_results",
                    "id": 207
                },
                {
                    "type": "pot_search_results",
                    "id": 208
                },
                {
                    "type": "pot_search_results",
                    "id": 209
                },
                {
                    "type": "pot_search_results",
                    "id": 210
                }
            ],
            "query_tree": null
        },
        "description": "hello example",
        "query_text": "Heat",
        "limit": 10,
        "error": null,
        "created_by": null,
        "href": "http://blabla/bla/bla/pot/searches/25"
    }
],
"linked": {
    "pot_search_results": [
        {
            "links": {
                "Pemash": {
                    "type": "pot_browse_contents",
                    "href": "http://blabla/bla/bla/pot/pots/5a267e98-0a12-4110-bac3-575439cb6097",
                    "id": "5a267e98-0a12-4110-bac3-575439cb6097"
                },
                "found_terms": [
                    {
                        "type": "pot_search_found_terms",
                        "id": 780
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 781
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 782
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 783
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 784
                    }
                ]
            },
            "id": 191,
            "score": 13.765055790063306
        },
        {
            "links": {
                "Pemash": {
                    "type": "pot_browse_contents",
                    "href": "http://blabla/bla/bla/pot/pots/5a267e98-0a12-4110-bac3-575439cb6097",
                    "id": "5a267e98-0a12-4110-bac3-575439cb6097"
                },
                "found_terms": [
                    {
                        "type": "pot_search_found_terms",
                        "id": 785
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 786
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 787
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 788
                    }
                ]
            },
            "id": 192,
            "score": 10.669084946432259
        },
        {
            "links": {
                "Pemash": {
                    "type": "pot_browse_contents",
                    "href": "http://blabla/bla/bla/pot/pots/5a267e98-0a12-4110-bac3-575439cb6097",
                    "id": "5a267e98-0a12-4110-bac3-575439cb6097"
                },
                "found_terms": [
                    {
                        "type": "pot_search_found_terms",
                        "id": 789
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 790
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 791
                    },
                    {
                        "type": "pot_search_found_terms",
                        "id": 792
                    }
                ]
            },
            "id": 193,
            "score": 10.669084946432259
        },

现在这是我的 VB.NET 代码:

Dim json As String = responseFromServer
Dim ser As JObject = JObject.Parse(json)

Dim data As List(Of JToken) = ser.Children().tolist() 'VisualStudio shows me error here that ToList is not a member of Newtonsoft.Json.Linq.JToken

Dim output As String = ""

这样我就无法将孩子保存在列表中。您能否告诉我如何使用 vb.net 访问“链接”子项下 json 结构中的 href 元素。

如果您提供一个示例,让我使用 vb.net 代码从 Json Structure 访问以下行,我将非常感谢您。

href": "http://blabla/bla/bla/pot/pots/5a267e98-0a12-4110-bac3-575439cb6097"

【问题讨论】:

  • 那一行显示什么错误?如果Children 是一个属性,而不是一个方法,那么您很可能不需要()。我已经有一段时间没有使用 JObject(我发现 Json.Net 更有用/更灵活)。
  • 我也试过这个但没有成功.......将数据作为 List(Of JToken) = ser.Children().ToList.... Visual Studio 显示错误“ ToList 不是 Newtonsoft.Json.Linq.JToken 的成员"
  • 您是否导入了System.Linq 命名空间?
  • 我添加了以下内容:> Imports System.IO Imports System.Net Imports System.Text Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq Imports System.Collections.Generic
  • 这不能回答我的问题。你有Imports System.Linq吗?

标签: json vb.net linq json.net


【解决方案1】:

ToList 方法是extension methodIEnumerable(Of T) 接口。它由 System.Core.dll 库在System.Linq 命名空间中定义。因此,除非您引用该库并导入该命名空间,否则您不会在任何可枚举对象上看到 ToList 方法。 ToList 不是由可枚举类型本身定义的方法。它是一个由该库自动附加到类型接口的方法,但仅在引用扩展它的库并导入命名空间时。

LINQ 及其所有扩展方法已添加到 .NET Framework 3.5 版本中,因此如果您的项目面向 3.0 或更早版本,您将无法访问其中任何一个。

【讨论】:

  • 非常感谢 Steven Doggart 的快速帮助。我非常感谢你。现在我可以为孩子们访问 ToList。现在我开发了用于访问 json 中的 href 值的程序。再次感谢亲爱的...下次见:)
  • 现在我无法访问“Pemash”项目。 :(。我尝试在这里发布我的代码。也许你可以帮助我。
  • 我建议将其作为一个新问题发布,而不是在此处添加。我可能无法帮助您,因为我对那个 JSON 库一无所知。如果您将其作为一个新问题提出,人们将更有可能看到它并为您提供帮助。
  • 没问题。祝你好运!
猜你喜欢
  • 2012-01-05
  • 2012-11-18
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多