VB6.0 组织json对象并生成json字符串

帮一客户修改VB6程序,需要将一些文本转换成json数组并输出。经过一番研究,决定用插件实现。

1. 安装插件Chilkat

Chilkat Full-Version Software Components Downloads (chilkatsoft.com)

选择 Chilkat ActiveX Components

win32和x64的版本,实测我的Win10 x64系统用win32版本是有效的。

2. 添加引用

工程 > 引用,勾选 Chilkat ActiveX v9.5.0, 确定

3. 代码

'组织数据格式》》》》》》》》》》》》
Dim textToShow As String
textToShow = TextLEDInfo.Text

Dim font As String
Dim color As String
Dim bgColor As String
Dim type1 As String
'格式统一设定
font = "2"
color = "0"
bgColor = "7"
type1 = "1"

Dim wordArray As New ChilkatJsonArray

For I = 0 To Len(textToShow) - 1
    Dim word As String
    word = Mid(textToShow, I + 1, 1) 'Mid()下标从1开始
    '组织json
    Dim success As Long
    success = wordArray.AddObjectAt(I)
    Dim obj As ChilkatJsonObject
    Set obj = wordArray.ObjectAt(I)
    success = obj.UpdateString("word", word)
    success = obj.UpdateString("font", font)
    success = obj.UpdateString("color", color)
    success = obj.UpdateString("bgColor", bgColor)
    success = obj.UpdateString("type", type1)
Next

'wordArray.EmitCompact = 0 换行,此处不需要
TextBoxContent.Text = wordArray.Emit()

效果:

 

实测有效,问题解决。
 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
在VB 6.0中生成JSON文件可以通过使用Microsoft Scripting Runtime库中的FileSystemObject对象来实现。以下是生成JSON文件的步骤: 1. 首先,在VB 6.0中打开一个新的工程或现有工程。 2. 在工程中添加对Microsoft Scripting Runtime库的引用。这可以通过选择“项目”菜单下的“引用”选项,然后选择“Microsoft Scripting Runtime”来完成。 3. 在代码中声明FileSystemObject对象,并创建一个Json文件的输出流。 ``` Dim objFSO As Scripting.FileSystemObject Dim objJsonFile As Scripting.TextStream Set objFSO = New Scripting.FileSystemObject Set objJsonFile = objFSO.CreateTextFile("output.json") ``` 4. 创建一个包含要写入JSON文件的数据的字典对象。 ``` Dim objData As Scripting.Dictionary Set objData = New Scripting.Dictionary objData.Add "name", "John Doe" objData.Add "age", 25 objData.Add "email", "[email protected]" ``` 5. 使用JsonConverter库中的方法将字典对象转换为JSON字符串,并将其写入JSON文件。 ``` Dim strJson As String Dim objJsonConverter As New JsonConverter strJson = objJsonConverter.ConvertToJson(objData) objJsonFile.Write strJson ``` 6. 完成后,确保关闭并释放所使用的对象。 ``` objJsonFile.Close Set objJsonFile = Nothing Set objFSO = Nothing ``` 通过以上步骤,VB 6.0中就成功生成了一个名为output.jsonJSON文件,其中包含定义的数据。请注意,以上代码示例中使用的JsonConverter库并非VB 6.0原生支持,你可以通过搜索并使用第三方库来实现JSON解析和转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鹰信息技术服务部

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值