我有这个字符串
?( {"entity": {"BusinessName": "FANEUIL HALL APARTMENTS, L.L.C.", "UBI": "601754321", "Category": "LLC", "Type": "Profit", "Active": "Active", "StateOfIncorporation": "WA", "DateOfIncorporation": "12/03/1996", "ExpirationDate": "12/31/2014", "DissolutionDate": "", "RegisteredAgentName": "LESLIE R PESTERFIELD", "RegisteredAgentAddress": "901 FIFTH AVE STE 3500", "RegisteredAgentCity": "SEATTLE", "RegisteredAgentState": "WA", "RegisteredAgentZip": "98164", "AlternateAddress": "", "AlternateCity": "", "AlternateState": "", "AlternateZip": "", "GoverningPersons": [ { "Title": "Manager", "LastName": "HEATHMAN", "MiddleName": "W", "FirstName": "MICHAEL W", "Address": "", "City": "PALM SPRINGS", "State": "CA", "Zip": " " } ]}} );
我的目标是让first name
,middle name
,last name
,city
和state
返回并排列到同一行的不同单元格中但不同的列。
我的代码适用于多个管理人员,但一旦成为一个管理人员,它就会停止按预期工作。
我无法在下面的代码
中计算我的Else
部分
Sub test()
Dim names As String
Values = Split(resp, """GoverningPersons"":")
If InStr(1, Values(1), "}, {") > 0 Then
newvalues = Split(Values(1), "}, {")
x = Split(newvalues(0), """LastName"": ")
j = Split(x(1), ", ""MiddleName"":")
m = Split(j(1), ", ""FirstName"":")
r = j(0) + m(0) + m(1)
l = Split(m(1), ", ""Address"": ")
names = l(0)
If InStr(1, l(0), " ") > 0 Then
al = Left(names, InStr(names, " ") - 1)
d = al + m(0) + j(0)
Else
d = l(0) + m(0) + j(0)
End If
a = Replace(d, Chr(32), "")
g = Replace(a, Chr(34), " ")
ElseIf InStr(1, Values(1), "[ }}") > 0 Then
Else
End If
End Sub
我尝试了所有我能想到的东西,这个名字永远不会像我代码的第一部分那样出现。
答案 0 :(得分:2)
这将帮助您入门。
我在这里向您展示了如何提取First name
,middle name
和last name
。我相信您可以应用相同的逻辑来提取city
和state
。
注意:我没有包含任何错误处理。我相信你会照顾好吗?
Sub Sample()
Dim resp As String
Dim Lastname As String, Midname As String, Firstname As String
'~~> For testing purpose, I stored the json string in Cell A1
resp = Range("A1").Value
'~~> This will give you last name
Lastname = GetValue(resp, """LastName"":")
Debug.Print "Lastname :" & Lastname
'~~> This will give you middle name
Midname = GetValue(resp, """MiddleName"":")
Debug.Print "Middle Name :" & Midname
'~~> This will give you First name
Firstname = GetValue(resp, """FirstName"":")
Debug.Print "First Name :" & Firstname
End Sub
Function GetValue(sString As String, sDelim As String) As String
Dim MyArray, tmpAr
MyArray = Split(sString, sDelim)
tmpAr = Split(MyArray(1), ",")
GetValue = Trim(Replace(tmpAr(0), Chr(34), ""))
End Function
<强>截图强>
Excel表格
VBA编辑
答案 1 :(得分:0)
这是另一种方法(需要一些javascript / JSON知识)
Sub tester()
'add project reference to "Microsoft script control"
Dim so As New ScriptControl
Dim i As Integer, num As Integer
Dim js As String, json As String
so.Language = "jscript"
json = "{""entity"": {""BusinessName"": ""FANEUIL HALL APARTMENTS, L.L.C."", " & _
" ""UBI"": ""601754321"", ""Category"": ""LLC"", ""Type"": ""Profit"", ""Active"": " & _
" ""Active"", ""StateOfIncorporation"": ""WA"", ""DateOfIncorporation"": " & _
" ""12/03/1996"", ""ExpirationDate"": ""12/31/2014"", ""DissolutionDate"": """", " & _
" ""RegisteredAgentName"": ""LESLIE R PESTERFIELD"", ""RegisteredAgentAddress"": " & _
" ""901 FIFTH AVE STE 3500"", ""RegisteredAgentCity"": ""SEATTLE"", " & _
" ""RegisteredAgentState"": ""WA"", ""RegisteredAgentZip"": ""98164"", " & _
" ""AlternateAddress"": """", ""AlternateCity"": """", ""AlternateState"": """", " & _
" ""AlternateZip"": """", ""GoverningPersons"": [ { ""Title"": ""Manager"", " & _
" ""LastName"": ""HEATHMAN"", ""MiddleName"": ""W"", ""FirstName"": ""MICHAEL W"", " & _
" ""Address"": """", ""City"": ""PALM SPRINGS"", ""State"": ""CA"", ""Zip"": """" } ]}}"
so.AddCode "var o = eval(" & json & ");"
num = so.Eval("o.entity.GoverningPersons.length") ' >> 1
For i = 0 To num - 1
js = "o.entity.GoverningPersons[" & i & "]."
Debug.Print so.Eval(js & "LastName;")
Debug.Print so.Eval(js & "FirstName;")
Debug.Print so.Eval(js & "MiddleName;")
'etc
Next i
End Sub