AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78821863
Accepted
Lord OfTheRing
Lord OfTheRing
Asked: 2024-08-01 23:43:00 +0800 CST2024-08-01 23:43:00 +0800 CST 2024-08-01 23:43:00 +0800 CST

VBA JsonConverter.ParseJson Parse Jason 给我错误类型不匹配

  • 772

我在字符串 groupsjasontext 中有以下 Json 值,我想使用 JsonConverter.ParseJson 解析 id、members.value、roles.value。我该怎么做?我尝试通过消息框显示 id 字段,但出现错误“类型不匹配”

    Set JsonData = JsonConverter.ParseJson(groupsjasontext)
    tempName = JsonData("id")(0)
    MsgBox (tempName)
    {
  "schemas": [
    "urn:ietf:params:scim:api:messages:2.0:ListResponse"
  ],
  "totalResults": 3,
  "startIndex": 1,
  "itemsPerPage": 3,
  "Resources": [
    {
      "schemas": [
        "urn:ietf:params:scim:schemas:core:2.0:Group",
        "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters"
      ],
      "id": "d3094970-ce7e-4794-b9e2-f84817b7c820",
      "meta": {
        "resourceType": "Group",
        "created": "2024-05-21T17:53:06.808Z",
        "lastModified": "2024-05-21T17:53:06.808Z",
        "location": "/api/v1/scim2/Groups/d3094970-ce7e-4794-b9e2-f84817b7c820"
      },
      "displayName": "AHI_VW_ALL_REGION",
      "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters": {
        "description": "View Mode ALL REGION"
      }
    },
    {
      "schemas": [
        "urn:ietf:params:scim:schemas:core:2.0:Group",
        "urn:sap:params:scim:schemas:extension:sac:2.0:group-roles",
        "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters"
      ],
      "id": "bef561ee-5a1e-420e-a1e\n4-4f624c96af6e",
      "meta": {
        "resourceType": "Group",
        "created": "2024-07-25T14:17:53.215Z",
        "lastModified": "2024-08-01T14:31:29.632Z",
        "location": "/api/v1/scim2/Groups/bef561ee-5a1e-420e-a1e4-4f624c96af6e"
      },
      "displayName": "TEAM_TEST",
      "members": [
        {
          "value": "f27dcbb9-df9a-46b2-b23a-3b35d5a8bdff",
          "type": "User",
          "display": "Test_FirstName1 TEST_Familyname1",
          "$ref": "/api/v1/scim2/Users/f27dcbb9-df9a-46b2-b23a-3b35d5a8bdff"
        }
      ],
      "urn:sap:params:scim:schemas:extension:sac:2.0:group-roles": {
        "roles": [
          {
            "value": "PROFILE:t.4:VIEW_USER",
            "display": "VIEW_USER"
          }
        ]
      },
      "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters": {
        "description": "Team Test"
      }
    },
    {
      "schemas": [
        "urn:ietf:params:scim:schemas:core:2.0:Group",
        "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters"
      ],
      "id": "1edd9227-c8de-486e-93f6-69396524c792",
      "meta": {
        "resourceType": "Group",
        "created": "2024-05-21T17:51:49.808Z",
        "lastModified": "2024-05-21T17:53:06.121Z",
        "location": "/api/v1/scim2/Groups/1edd9227-c8de-486e-93f6-69396524c792"
      },
      "displayName": "AHI_VW_EURAM\n",
      "urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters": {
        "description": "View mode EURAM"
      }
    }
  ]
}
json
  • 3 3 个回答
  • 57 Views

3 个回答

  • Voted
  1. Haluk
    2024-08-02T00:39:39+08:002024-08-02T00:39:39+08:00

    使用 JSonParser,可以从示例 JSON 字符串中提取所需的值,如下所示;

    Sub Test()
        Dim strJSON As String, JSon As Object, id As String, membersValue As String, rolesValue As String
        
        strJSON = "{""schemas"":[""urn:ietf:params:scim:schemas:core:2.0:Group"",""urn:sap:params:scim:schemas:extension:sac:2.0:group-roles"",""urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters""],""id"":""bef561ee-5a1e-420e-a1e\n4-4f624c96af6e"",""meta"":{""resourceType"":""Group"",""created"":""2024-07-25T14:17:53.215Z"",""lastModified"":""2024-08-01T14:31:29.632Z"",""location"":""/api/v1/scim2/Groups/bef561ee-5a1e-420e-a1e4-4f624c96af6e""},""displayName"":""TEAM_TEST"",""members"":[{""value"":""f27dcbb9-df9a-46b2-b23a-3b35d5a8bdff"",""type"":""User"",""display"":""Test_FirstName1 TEST_Familyname1"",""$ref"":""/api/v1/scim2/Users/f27dcbb9-df9a-46b2-b23a-3b35d5a8bdff""}],""urn:sap:params:scim:schemas:extension:sac:2.0:group-roles"":{""roles"":[{""value"":""PROFILE:t.4:VIEW_USER"",""display"":""VIEW_USER""}]},""urn:sap:params:scim:schemas:extension:sac:2.0:group-custom-parameters"":{""description"":""Team Test""}}"
          
        Set JSon = ParseJson(strJSON)
    
        id = JSon("id")
        
        membersValue = JSon("members")(1)("value")
        
        rolesValue = JSon("urn:sap:params:scim:schemas:extension:sac:2.0:group-roles")("roles")(1)("value")
    
        
        MsgBox "id = " & id & vbCrLf & "members.value = " & membersValue & vbCrLf & "roles.value = " & rolesValue
    End Sub
    
    
    • 1
  2. Tim Williams
    2024-08-02T00:40:21+08:002024-08-02T00:40:21+08:00

    键id与单个值相关联,而不是对象或数组,因此

    tempName = JsonData("id")(0)
    MsgBox (tempName)   'no parens as noted in BigBen's comment
    

    应该

    tempName = JsonData("id")
    MsgBox tempName
    
    • 1
  3. Best Answer
    PeterT
    2024-08-02T03:46:24+08:002024-08-02T03:46:24+08:00

    我发现在每个步骤中单独访问 JSON 对象的不同部分要容易得多。这有助于避免大多数问题以及不同对象(字典和集合)之间的混淆。我一直使用这个参考资料,事实证明它有很大帮助。

    因此,根据该建议并使用链接参考,我提出了下面的测试代码来展示如何访问 JSON 结构的不同部分并在适当的时候执行循环。

    Option Explicit
    
    Sub test()
        Dim strJSON As String
        strJSON = GetJSONString
        
        Dim json As Object
        Set json = ParseJson(strJSON)
        
        Dim resources As Collection
        Set resources = GetCollection(json, "Resources")
        
        Dim junk As Collection
        Set junk = GetCollection(json, "junk")
        
        Dim resource As Dictionary
        For Each resource In resources
            Debug.Print "----- Resource:"
            Dim schemas As Collection
            Set schemas = GetCollection(resource, "schemas")
            
            Dim i As Long
            For i = 1 To schemas.Count
                Debug.Print "   Schema(" & i & ") = " & schemas(i)
            Next i
            
            Dim id As String
            id = resource("id")
            
            Dim meta As Dictionary
            Set meta = GetDictionary(resource, "meta")
            If Not meta Is Nothing Then
                Dim metaEntry As Variant
                For Each metaEntry In meta
                    Debug.Print "   Meta Entry: " & metaEntry & " = " & meta(metaEntry)
                Next metaEntry
            End If
            
            Dim displayName As String
            displayName = resource("displayName")
            
            Dim members As Collection
            Set members = GetCollection(resource, "members")
            If Not members Is Nothing Then
                Dim member As Variant
                Debug.Print "   member count = " & members.Count
                For i = 1 To members.Count
                    Dim memberEntry As Dictionary
                    Set memberEntry = members(i)
                    Dim entryKey As Variant
                    For Each entryKey In memberEntry
                        Debug.Print "   For member(" & i & "): " & entryKey & " = " & memberEntry(entryKey)
                    Next entryKey
                Next i
            End If
        Next resource
    End Sub
    
    Function GetCollection(ByRef jsonObj As Object, ByVal entryName As String) As Collection
        On Error Resume Next
        Set GetCollection = jsonObj(entryName)
        If GetCollection Is Nothing Then
            Debug.Print "The requested object " & entryName & " does not exist in the referenced Collection"
        End If
    End Function
    
    Function GetDictionary(ByRef jsonObj As Object, ByVal entryName As String) As Dictionary
        On Error Resume Next
        Set GetDictionary = jsonObj(entryName)
        If GetDictionary Is Nothing Then
            Debug.Print "The requested object " & entryName & " does not exist in the referenced Dictionary"
        End If
    End Function
    
    Function GetJSONString() As String
        Dim fileHandle As Integer
        Dim filename As String
        filename = "C:\temp\test.json"
        fileHandle = FreeFile
        Open filename For Input As FreeFile
        GetJSONString = Input(LOF(fileHandle), fileHandle)
        Close fileHandle
    End Function
    
    • 0

相关问题

  • PostgreSQL:如何验证 JSON 字段?

  • jq 对特定键进行过滤并将值整理到单个 csv 单元格中

  • Elasticsearch:在 2 节点集群中创建副本时出错

  • 未进行任何更改的行上的 Sales Apex 错误消息

  • 如何使用XSLT 3.0关键功能

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve