0

我将如何解析这个 Json 字符串?

'{ ' "领土": { ' "RSC": { ' "sector": 1, ' "size": 3, ' "density": 2, ' "slots": 18, ' "daily_respect": 113, ' “派系”:13784,'“coordinate_x”:“3199.2”,'“coordinate_y”:“2828.32”,'},'“HSC”:{'“扇区”:1,'“大小”:4,'“密度": 3, ' "插槽": 26, '"daily_respect": 197, ' "faction": 13784, ' "coordinate_x": "3457.41", ' "coordinate_y": "2785.98", ' }, ' "JSC": { ' "sector": 1, ' "size ": 2, ' "密度": 3, ' "slots": 8, ' "daily_respect": 141, ' "faction": 13784, ' "coordinate_x": "3479.67", ' "coordinate_y": "2819.67", ' }, '"NTC": { ' "sector": 1, ' "size": 3, ' "density": 2, ' "slots": 21, ' "daily_respect": 113, ' "faction": 13784, ' "coordinate_x" : "3416.45", ' "coordinate_y": "2868.95", ' }, ' "OTC": { ' "sector": 1, ' "size": 4, ' "density": 3, ' "slots": 24 , ' "daily_respect": 197, ' "派系”:13784,'“coordinate_x”:“3417.68”,'“coordinate_y”:“2904.79”,'},'“QTC”:{'“扇区”:1,'“大小”:3,'“密度” : 2, ' "slots": 12, ' "daily_respect": 113, ' "faction": 13784, ' "coordinate_x": "3395.34", ' "coordinate_y": "3039.22", ' }, ' "RTC": { ' "扇区": 1, '"size": 3, ' "density": 2, ' "slots": 16, ' "daily_respect": 113, ' "faction": 13784, ' "coordinate_x": "3366.33", ' "coordinate_y": "3012.11 ", ' }, ' "TTC": { ' "sector": 1, ' "size": 4, ' "density": 3, ' "slots": 28, ' "daily_respect": 197, ' "faction" : 13784, ' "坐标x":"3244.33",'"坐标y":"2799.8",'},'"UTC":{'"扇区":1,'"大小":3,'"密度":2,'"槽" : 12, ' "daily_respect": 113, ' "faction": 13784, ' "coordinate_x": "3278.81", ' "coordinate_y": "2756.83", ' "racket": { ' "name": "Truck Stop II ", ' "等级": 2, '"reward": "20x Can of Munster daily", ' "created": 1604663824, ' "changed": 1645588625, ' }, ' },

Dim response As Object
Set response = JsonConverter.ParseJson(request.responseText)
Dim iCounter            As Integer
Dim RCounter            As Integer
Dim CCounter            As Integer
iCounter = 1
Dim territory As Dictionary
Set territory = response("territory")
Dim Block_id As Variant
For Each Block_id In territory

Debug.Print Block_id

Sheet7.Range("a115").Offset(iCounter, 0).Value = Block_id
    For Each sector In Block_id
Sheet7.Range("a115").Offset(iCounter, 1).Value = sector
        Next sector
iCounter = iCounter + 1
Next Block_id

块 ID 打印正确,但不是“扇区”。对于尺寸,密度,插槽等,我必须相同,而“球拍”及其物品则需要另一个级别。我一直在研究字典和收藏,但无法弄清楚。请帮忙!

4

1 回答 1

2

因此,在重新格式化和修复您的 JSON 之后,我设法提供了一个关于如何处理数据的基本示例。

我没有对此进行任何递归,但根据您希望它的动态程度,您需要做一些进一步的工作。

我将您的 JSON 复制到一个文本文件中,然后读取它并遍历数据。这是一个如何读取所有数据并将其写入 VBA 编辑器中的即时窗口的示例。

我使用了For Each和的组合For来向您展示它是如何工作的。

Public Sub Test()
    Dim objFSO As Scripting.FileSystemObject, objStream As Scripting.TextStream
    Dim strJSON As String, i As Long, x As Long, y As Long
    
    Dim objData As Scripting.Dictionary
    Dim objTerritories As Scripting.Dictionary, strTerritory As Variant
    Dim objTerritory As Scripting.Dictionary, strProperty As Variant
    Dim varPropertyValue As String
    Dim objRacket As Scripting.Dictionary
    
    Set objFSO = New Scripting.FileSystemObject
    Set objStream = objFSO.OpenTextFile("c:\temp\json.txt")
    
    strJSON = objStream.ReadAll
    
    objStream.Close
    
    Set objStream = Nothing
    Set objFSO = Nothing
    
    Set objData = JsonConverter.ParseJson(strJSON)
    Set objTerritories = objData("territory")
    
    For Each strTerritory In objTerritories.Keys
        Debug.Print strTerritory
        
        Set objTerritory = objTerritories(strTerritory)
        
        For Each strProperty In objTerritory.Keys
            Select Case TypeName(objTerritory(strProperty))
                Case "Dictionary"
                    Set objRacket = objTerritory(strProperty)
                    Debug.Print "... " & strProperty & ":"
                    
                    For y = 0 To objRacket.Count - 1
                        strProperty = objRacket.Keys(y)
                        
                        varPropertyValue = objRacket(strProperty)
                        Debug.Print "...... " & strProperty & " = " & varPropertyValue
                    Next
                    
                Case Else
                    varPropertyValue = objTerritory(strProperty)
                    Debug.Print "... " & strProperty & " = " & varPropertyValue
                    
            End Select
        Next
    Next
End Sub

我不能给你一个确切的答案,因为我不知道你想用它做什么,但你应该能够根据需要进行调整。

出于参考目的,这是一次重新格式化和美化的 JSON ...

{
  "territory": {
    "RSC": {
      "sector": 1,
      "size": 3,
      "density": 2,
      "slots": 18,
      "daily_respect": 113,
      "faction": 13784,
      "coordinate_x": "3199.2",
      "coordinate_y": "2828.32"
    },
    "HSC": {
      "sector": 1,
      "size": 4,
      "density": 3,
      "slots": 26,
      "daily_respect": 197,
      "faction": 13784,
      "coordinate_x": "3457.41",
      "coordinate_y": "2785.98"
    },
    "JSC": {
      "sector": 1,
      "size": 2,
      "density": 3,
      "slots": 8,
      "daily_respect": 141,
      "faction": 13784,
      "coordinate_x": "3479.67",
      "coordinate_y": "2819.67"
    },
    "NTC": {
      "sector": 1,
      "size": 3,
      "density": 2,
      "slots": 21,
      "daily_respect": 113,
      "faction": 13784,
      "coordinate_x": "3416.45",
      "coordinate_y": "2868.95"
    },
    "OTC": {
      "sector": 1,
      "size": 4,
      "density": 3,
      "slots": 24,
      "daily_respect": 197,
      "faction": 13784,
      "coordinate_x": "3417.68",
      "coordinate_y": "2904.79"
    },
    "QTC": {
      "sector": 1,
      "size": 3,
      "density": 2,
      "slots": 12,
      "daily_respect": 113,
      "faction": 13784,
      "coordinate_x": "3395.34",
      "coordinate_y": "3039.22"
    },
    "RTC": {
      "sector": 1,
      "size": 3,
      "density": 2,
      "slots": 16,
      "daily_respect": 113,
      "faction": 13784,
      "coordinate_x": "3366.33",
      "coordinate_y": "3012.11"
    },
    "TTC": {
      "sector": 1,
      "size": 4,
      "density": 3,
      "slots": 28,
      "daily_respect": 197,
      "faction": 13784,
      "coordinate_x": "3244.33",
      "coordinate_y": "2799.8"
    },
    "UTC": {
      "sector": 1,
      "size": 3,
      "density": 2,
      "slots": 12,
      "daily_respect": 113,
      "faction": 13784,
      "coordinate_x": "3278.81",
      "coordinate_y": "2756.83",
      "racket": {
        "name": "Truck Stop II",
        "level": 2,
        "reward": "20x Can of Munster daily",
        "created": 1604663824,
        "changed": 1645588625
      }
    }
  }
}
于 2022-02-27T10:57:40.553 回答