使用JsonConverter输入类型不匹配

使用JsonConverter输入类型不匹配

本文介绍了Excel VBA运行时错误'13':使用JsonConverter输入类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在"Excel VBA运行时错误'13':类型不匹配"中获取错误;使用JsonConverter

Getting Error on "Excel VBA Run-time error '13': Type mismatch" using JsonConverter

我的JSON

{"gstin":"33A","fp":"062020","b2b":[{"ctin":"33B","cfs":"Y","cfs3b":"Y","inv":[{"itms":[{"num":1801,"itm_det":{"csamt":0,"samt":83.97,"rt":18,"txval":933,"camt":83.97}}],"val":1050.94,"inv_typ":"R","pos":"33","idt":"10-06-2020","rchrg":"N","inum":"C3/071","chksum":"60a9044051e8b6ba1122f614143a4d1236b1399872b0ea408df6a82ba832253d"}],"fldtr1":"25-Jul-20","flprdr1":"Jun-20"}]}

我的代码

Private Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select Json files"
.AllowMultiSelect = False

    If .Show() Then
        Filename = .SelectedItems(1)
        Dim content As String
        Dim iFile As Integer: iFile = FreeFile
        Open Filename For Input As #iFile
            content = Input(LOF(iFile), iFile)
            Dim products As Object, Item

            Set products = JsonConverter.ParseJson(content)
            i = 1


            For Each Item In products

                Debug.Print Item("gstin")
                'Cells(i, 1) = Item("ctin")
                'i = i + 1
            Next
        Close #iFile
    End If
 End With
 End Sub

还需要实现root和key(例如:gstin,ctin,csamt,inum)

also need to implement root and keys (like: gstin, ctin, csamt, inum)

谢谢

推荐答案

请限制为一个问题.您当前的错误是因为产品是字典,键是字符串.您不能执行Item("gstin"),因为Item是一个字符串.您最初希望使用product(Item),但不能仅使用Debug.Print,因为并非字典中的所有关联值都是简单的数据类型,例如产品("b2b")将返回一个集合,由于语法错误而导致RTE 450错误.

Please limit to a single question. Your current error is because products is a dictionary and the keys are strings. You cannot do Item("gstin") as Item is a string. You would want initially products(Item) but won't be able to just use Debug.Print as not all the associated values in the dictionary are simple datatypes e.g. products("b2b") will return a collection and lead to a RTE 450 error due to incorrect syntax.

您将需要开发代码以测试从字典返回的数据类型以及任何嵌套级别.[]表示您可以针对每个集合,而{}表示字典.SO上有很多示例可以帮助您解决这些问题,而代码示例将为您写出整个结构.

You will need to develop your code to test for which datatype is returned from the dictionary and any nested levels. [] indicates a collection you can For Each over, whereas {} indicates a dictionary. There are lots of examples on SO to help you with this and code examples that will write the entire structure out for you.

这篇关于Excel VBA运行时错误'13':使用JsonConverter输入类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:02