我试图解析JSON字符串以提取一个项目,但遇到了一些麻烦。
这是解析我存储在名为result
的变量中的JSON字符串:
{"From":{"Chocolate":{"Price":1.0,"AsAtDate":"2018-05-04T00:00:00"},"Lime":{"Price":1.35415115,"AsAtDate":"2018-05-04T00:00:00"},"Strawberry":{"Price":1.19517151,"AsAtDate":"2018-05-04T00:00:00"},"Vanilla":{"Price":0.77522986,"AsAtDate":"2018-05-04T00:00:00"},"Blueberry":{"Price":1.00084071,"AsAtDate":"2018-05-04T00:00:00"},"Lemon":{"Price":0.75030012,"AsAtDate":"2018-05-04T00:00:00"}},"To":"Chocolate","RequestedDate":"2018-05-22T08:26:16"}
我试图提取的只是" Price"。我试图通过for循环运行它 -
Dim result As String
Dim Item As Variant
Dim parsedResult As Object
Dim i As Long
Dim ws As Worksheet
result = objHTTP.responseText
Set ws = Worksheets("Sheet1")
Set parsedResult = JsonConverter.ParseJson(result)
i = 1
For Each Item In parsedResult("From")
ws.Cells(i, 2) = Item("Price")
i = i + 1
Next
我一直持续不断地在
处出现type mismatch
错误
ws.Cells(i,2)= Item(" Price")
我在这里定义错了吗?我路由result
以确保我正在获取正确的JSON字符串进行解析(如上所示),所以到目前为止所做的一切都很棒。
答案 0 :(得分:1)
尝试用
替换失败的行import json
import re
json_string = """
[{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":null,"1":"+ 110 \u20ac Bonus zum Anbieter","2":"+ Wettbonus zum Anbieter","3":
"+ 100 \u20ac Bonus zum Anbieter","4":"+ 100 \u20ac Bonus zum Anbieter","5":"+ 100 \u20ac Bonus zum Anbieter","6":"+ 100 \u20ac Bonus zum Anbieter","7":"+ 150 \u20ac Bonus zum Anbieter","8":"+ 150 \u20ac Bonus zum Anbieter"},{"0":"Bayern M\u00fcnchen \u2013 VfB Stuttgart","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":nul
l,"8":null},{"0":"FC Bayern","1":"120","2":"116","3":"120","4":"117","5":"118","6":"120","7":"120","8":"120"},{"0":"Unentschieden","1":"700","2":"800","3":"700","4":"8
30","5":"850","6":"600","7":"800","8":"800"},{"0":"VfB Stuttgart","1":"125","2":"150","3":"120","4":"145","5":"140","6":"150","7":"110","8":"120"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"TSG Hoffenheim \u2013 Borussia Dortmund","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7"
:null,"8":null},{"0":"Hoffenheim","1":"240","2":"220","3":"220","4":"224","5":"220","6":"225","7":"220","8":"220"},{"0":"Unentschieden","1":"370","2":"380","3":"380","
4":"380","5":"400","6":"350","7":"380","8":"400"},{"0":"Dortmund","1":"270","2":"300","3":"300","4":"295","5":"300","6":"300","7":"300","8":"300"},{"0":null,"1":null,"
2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Hertha BSC \u2013 RB Leipzig","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":n
ull},{"0":"Hertha","1":"450","2":"420","3":"410","4":"395","5":"410","6":"380","7":"380","8":"420"},{"0":"Unentschieden","1":"360","2":"375","3":"400","4":"400","5":"4
00","6":"400","7":"420","8":"420"},{"0":"RB Leipzig","1":"180","2":"185","3":"180","4":"184","5":"183","6":"183","7":"183","8":"185"},{"0":null,"1":null,"2":null,"3":n
ull,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"SC Freiburg \u2013 FC Augsburg","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
Freiburg","1":"200","2":"195","3":"195","4":"200","5":"190","6":"200","7":"195","8":"190"},{"0":"Unentschieden","1":"310","2":"340","3":"340","4":"340","5":"360","6":"
320","7":"350","8":"360"},{"0":"Augsburg","1":"430","2":"420","3":"410","4":"395","5":"400","6":"400","7":"400","8":"450"},{"0":null,"1":null,"2":null,"3":null,"4":nul
l,"5":null,"6":null,"7":null,"8":null},{"0":"Schalke 04 \u2013 Eintracht Frankfurt","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Scha
lke","1":"250","2":"250","3":"250","4":"250","5":"250","6":"240","7":"250","8":"220"},{"0":"Unentschieden","1":"340","2":"350","3":"340","4":"340","5":"350","6":"340","7":"340","8":"380"},{"0":"E. Frankfurt","1":"275","2":"275","3":"270","4":"280","5":"287","6":"280","7":"280","8":"310"},{"0":null,"1":null,"2":null,"3":null,"4":null
,"5":null,"6":null,"7":null,"8":null},{"0":"Bayer 04 Leverkusen \u2013 Hannover 96","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Leve
rkusen","1":"117","2":"120","3":"120","4":"122","5":"118","6":"122","7":"122","8":"127"},{"0":"Unentschieden","1":"750","2":"700","3":"700","4":"740","5":"750","6":"60
0","7":"750","8":"800"},{"0":"Hannover 96","1":"150","2":"130","3":"130","4":"110","5":"140","6":"130","7":"100","8":"100"},{"0":null,"1":null,"2":null,"3":null,"4":nu
ll,"5":null,"6":null,"7":null,"8":null},{"0":"Hamburger SV \u2013 M\u00f6nchengladbach","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"
HSV","1":"220","2":"230","3":"225","4":"234","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"370","2":"350","3":"370","4":"370","5":"375","6":"320",
"7":"380","8":"420"},{"0":"Gladbach","1":"300","2":"300","3":"300","4":"285","5":"300","6":"300","7":"287","8":"340"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"1. FSV Mainz 05 \u2013 Werder Bremen","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"Mainz 05",
"1":"230","2":"230","3":"230","4":"236","5":"230","6":"240","7":"230","8":"200"},{"0":"Unentschieden","1":"340","2":"360","3":"360","4":"365","5":"375","6":"320","7":"
375","8":"380"},{"0":"Werder Bremen","1":"305","2":"300","3":"300","4":"285","5":"290","6":"300","7":"287","8":"290"},{"0":null,"1":null,"2":null,"3":null,"4":null,"5"
:null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsburg \u2013 1. FC K\u00f6ln","1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null},{"0":"VfL Wolfsb
urg","1":"170","2":"165","3":"160","4":"164","5":"161","6":"167","7":"165","8":"170"},{"0":"Unentschieden","1":"350","2":"400","3":"430","4":"420","5":"433","6":"375",
"7":"420","8":"420"},{"0":"1. FC K\u00f6ln","1":"550","2":"525","3":"530","4":"505","5":"500","6":"500","7":"475","8":"500"},{"0":null,"1":null,"2":null,"3":null,"4":n
ull,"5":null,"6":null,"7":null,"8":null}]
"""
json_string = re.sub("\n", "", json_string)
a_list = json.loads(json_string)
print(a_list[0])
答案 1 :(得分:1)
我知道这很容易通过Json解析器实现,但为了演示如何从嵌套字典中获取结果,请参阅以下内容:
Set dict1 = parsedResult
For Each Key1 In dict1
Set dict2 = dict1("From")
For Each Key2 In dict2
Set dict3 = dict2(Key2)
For Each Key3 In dict3
Debug.Print dict3("Price")
Next Key3
Next Key2
Next Key1