解析具有相同属性但“名称"不同的 JSON 文件 [英] Parsing a JSON File with same properties but different "Names"

查看:41
本文介绍了解析具有相同属性但“名称"不同的 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSON 新手,无法解决以下问题:

I'm new to JSON and can't wrap my head around the following problem:

我从第三方程序获取 JSON 文件.JSON 如下所示:

I'm getting a JSON file from a third party program. The JSON looks like this:

{
"Name 1": {
    "Property 1": 154600,
    "Property 2": true,
    "Property 3": 5340,
    "Property 4": 54634
  },
  "Name 2": {
    "Property 1": 5436,
    "Property 2": false,
    "Property 3": 45678769,
    "Property 4": 2342342
  }
}

我需要将它们放在一个 Listview 中,每个属性和名称都有一列.

I need to get them in a Listview with a column for every Property and the Name.

我使用 JSONUtils 网站生成了一个类:

I generated a class with the JSONUtils website:

    Public Class Name1

        <JsonProperty("Property 1")>
        Public Property Property1 As Integer

        <JsonProperty("Property 2")>
        Public Property Property2 As Boolean

        <JsonProperty("Property 3")>
        Public Property Property3 As Integer

        <JsonProperty("Property 4")>
        Public Property Property4 As Integer
    End Class

    Public Class Name2

        <JsonProperty("Property 1")>
        Public Property Property1 As Integer

        <JsonProperty("Property 2")>
        Public Property Property2 As Boolean

        <JsonProperty("Property 3")>
        Public Property Property3 As Integer

        <JsonProperty("Property 4")>
        Public Property Property4 As Integer
    End Class

    Public Class NameList

        <JsonProperty("Name 1")>
        Public Property Name1 As Name1

        <JsonProperty("Name 2")>
        Public Property Name2 As Name2
    End Class

但是由于名称的变化,我必须在运行时动态添加这些类.

But because of the changing Names I would have to dynamically add those classes on runtime.

接下来我尝试生成一个看起来像这样的类:

Next I tried to generate a class which looks like this:

    Public Class NameList

        Public Property Name As String

        <JsonProperty("Property 1")>
        Public Property Property1 As Integer

        <JsonProperty("Property 2")>
        Public Property Property2 As Boolean

        <JsonProperty("Property 3")>
        Public Property Property3 As Integer

        <JsonProperty("Property 4")>
        Public Property Property4 As Integer
    End Class

这符合预期,但是当我运行测试程序时,出现错误:

That would be as intended, but when i run the testprogram, I'm getting an error:

System.NullReferenceException: "Object reference not set to an instance of an object."

在我的代码下面:

Dim client As New WebClient()
Dim stream As Stream = client.OpenRead("C:\\Filename.json")
Dim reader As New StreamReader(stream)
Dim jsonData As String = reader.ReadToEnd

reader.Close()

Dim allData As JObject = JObject.Parse(jsonData)

Dim nameDataList As New List(Of NameList)

For Each token As JToken In allData("")

      Dim prop As JProperty = token

      If Not prop.Name.StartsWith("_") Then
          nameDataList.Add(New NameList With {
              .Name = prop.Name,
              .Property1 = prop.Value("Property 1"),
              .Property2 = prop.Value("Property 2"),
              .Property3 = prop.Value("Property 3"),
              .Property4 = prop.Value("Property 4")})
      End If

Next

之后是将令牌添加到我的 Listview 的代码.

After that comes the code to add the token to my Listview.

错误出现在这一行:

For Each token As JToken In allData("")

我怎样才能避免这个错误?

How can i avoid this error?

提前致谢:)

推荐答案

allData("") 没有任何 JToken.它是一个 [string, JToken] 的 KeyValuePair.

The allData("") does not have any JTokens. Its a KeyValuePair of [string, JToken].

尝试从代码中删除 ("") 和 As JToken.像这样:

Try removing the ("") from your code, and the As JToken. Like so:

    For Each obj As KeyValuePair In allData
        Dim name As String = obj.Key
        Dim token As JToken = obj.Value
    Next

https://dotnetfiddle.net/4ZED9z

这篇关于解析具有相同属性但“名称"不同的 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆