You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET遍历JSON数组遇阻,求专业技术指导

解决VB遍历嵌套JSON数组的问题

第一步:修复JSON语法错误

你提供的JSON存在多处语法问题,无法被正常解析:

  • 最外层的{[Values, {[不符合JSON规范,JSON对象必须是键值对,应修正为{"Values": [
  • 数组末尾的多余逗号(如第二个对象数组最后一个元素后的逗号)需要删除
  • 最外层的闭合括号不匹配,需修正为正确的]}

修正后的JSON如下:

{
  "Values": [
    [
      {
        "Axis": "1",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Acceleration",
        "FileID": 1326,
        "Unit": 0,
        "Value": 0.0547971
      },
      {
        "Axis": "2",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Acceleration",
        "FileID": 1326,
        "Unit": 0,
        "Value": 0.0253521
      },
      {
        "Axis": "3",
        "Channel": -1,
        "Date": "2022/10/20 12:45:48",
        "DescUnit": "Acceleration",
        "FileID": 1326,
        "Unit": 0,
        "Value": 0.0194674
      }
    ],
    [
      {
        "Axis": "1",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Velocity",
        "FileID": 1326,
        "Unit": 2,
        "Value": 1.07888
      },
      {
        "Axis": "2",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Velocity",
        "FileID": 1326,
        "Unit": 2,
        "Value": 0.275461
      },
      {
        "Axis": "3",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Velocity",
        "FileID": 1326,
        "Unit": 2,
        "Value": 0.196169
      }
    ],
    [
      {
        "Axis": "1",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Acceleration Envelope",
        "FileID": 1326,
        "Unit": 6,
        "Value": 0.0292101
      },
      {
        "Axis": "2",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Acceleration Envelope",
        "FileID": 1326,
        "Unit": 6,
        "Value": 0.0187995
      },
      {
        "Axis": "3",
        "Channel": -1,
        "Date": "2022/10/20 00:09:47",
        "DescUnit": "Acceleration Envelope",
        "FileID": 1326,
        "Unit": 6,
        "Value": 0.0122969
      }
    ]
  ]
}

第二步:正确遍历嵌套JSON数组的VB代码

原代码错误在于将Values下的数组元素当作JProperty处理,但实际上Values对应的是嵌套数组(数组包含多个子数组,每个子数组内是数据对象)。正确的遍历需要两层循环:

' 解析修正后的JSON字符串
Dim j As JObject = JObject.Parse(jsonString)

' 获取Values对应的外层数组
Dim valuesArray As JArray = j("Values")

' 遍历外层数组中的每个子数组
For Each subArray As JArray In valuesArray
    ' 遍历子数组中的每个数据对象
    For Each item As JObject In subArray
        ' 按需输出对象字段,这里示例输出Axis、DescUnit和Value
        Debug.Print($"Axis: {item("Axis")}, 类型: {item("DescUnit")}, 值: {item("Value")}")
    Next
Next

关键说明

  • JObject.Parse仅能解析语法正确的JSON,因此必须先修正JSON的语法错误
  • Values是顶级对象的属性,对应的值是JArray类型,而非单个属性或对象
  • 由于是嵌套数组结构,需要两层循环:第一层遍历外层子数组,第二层遍历子数组内的数据对象

内容的提问来源于stack exchange,提问作者user3786542

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:15:32