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

如何在VB6中解析JSON并将数据单独显示到文本框

问题:VBA-JSON解析嵌套JSON数据失败

我从JSON文件获取数据,希望将部分数据单独显示在文本框中,正在使用VBA-JSON v2.3.1 JsonConverter。以下是JSON输出示例:

{"status": "OK",
"code": "OK",
"message": "Ok",
"data": {
"user": {
  "id": 5883,
  "username": "Demo",
  "name": "Niki Jackson",
  "email": "abcd@yahoo.com",
  "mobile": "9325547989",
  "certificate_id": "122876",
  "address": "Sunshine Bvl",
  "company": "Light Co",
  "expire": "0001-01-01T00:00:00Z",
  "status": "active"
  }
 }
}

我使用以下代码但无法正常运行:

Dim Parsed As Object
Set Parsed = JsonConverter.ParseJson(JSONS)
text1.text = Parsed("data")("user")(1)("name")
text2.text = Parsed("data")("user")(1)("mobile") 

请问如何正确解析JSON数据?请提供基于JsonConverter的代码,感谢。


解决方法

错误原因

代码中错误使用了(1)索引,因为JSON里的user是键值对对象(用大括号{}包裹),而非数组(数组用方括号[]包裹)。VBA-JSON中,对象需通过键名直接访问,不需要索引。

正确代码

Dim Parsed As Object
Set Parsed = JsonConverter.ParseJson(JSONS)

' 直接通过键名访问user对象下的属性
text1.Text = Parsed("data")("user")("name")
text2.Text = Parsed("data")("user")("mobile")

' 可选:如果需要批量访问user的属性,可以先把user对象单独取出
' Dim userObj As Object
' Set userObj = Parsed("data")("user")
' text1.Text = userObj("name")
' text2.Text = userObj("mobile")

注意事项

  1. 确保已在VBA项目中导入JsonConverter.bas模块
  2. 确认JSONS变量是包含完整有效JSON内容的字符串
  3. 只有当user是数组格式(比如"user": [{...}])时,才需要用(1)索引访问第一个元素

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 04:32:36