• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Unable to parse the name from json response

shahin

Active Member
This time I tried to parse data from json response using string manipulation. Upon running my script. I get nothing as result. Apparently, it seems to me that I did it the right way. However, a little twitch on my code might lead me to the right direction. Thanks in advance. Here is the link to that :"http://jsonplaceholder.typicode.com/users" and this is what I tried:
Code:
Sub Json_data()
Dim http As New XMLHTTP60, itm As Variant
    With http
        .Open "GET", "http://jsonplaceholder.typicode.com/users", False
        .send
        itm = Split(.responseText, "id"":")
    End With
    x = UBound(itm)
    On Error Resume Next
    For y = 1 To x
        Cells(y, 1) = Split(Split(str(y), "name"":"" """)(1), """")(0)
        Cells(y, 2) = Split(Split(str(y), "username"":"" """)(1), """")(0)
    Next y
End Sub
 
Hi ,

Try this :
Code:
Sub Json_data()
    Dim http As New XMLHTTP60, itm As Variant
   
    With http
        .Open "GET", "http://jsonplaceholder.typicode.com/users", False
        .send
        itm = Split(.responseText, "id"":")
    End With
   
    x = UBound(itm)
   
    On Error Resume Next
   
    For y = 1 To x
        Cells(y, 1) = Split(Split(itm(y), " " & y & "," & Chr(10) & "    ""name"": """)(1), """")(0)
        Cells(y, 2) = Split(Split(itm(y), "username"": """)(1), """")(0)
    Next y
End Sub
Narayan
 
Thanks a lot Narayan, for your robust, flawless and effective answer. It is very soothing that I always found you quick and precise. I tried with my code several times with many different way before your intervention but it could not bring any result cause I copied and pasted the gist portion of my code to save time and mistakenly kept that "str" unchanged. Thanks once again.
 
Here is the complete code to parse data from the link:
Code:
Sub Json_coder()
Dim http As New XMLHTTP60, itm As Variant
    With http
        .Open "GET", "http://jsonplaceholder.typicode.com/users", False
        .send
        itm = Split(.responseText, "id"":")
    End With
    x = UBound(itm)

    For y = 1 To x
        Cells(y, 1) = Split(Split(itm(y), "name"": """)(1), """")(0)
        Cells(y, 2) = Split(Split(itm(y), "username"": """)(1), """")(0)
        Cells(y, 3) = Split(Split(itm(y), "email"": """)(1), """")(0)
        Cells(y, 4) = Split(Split(itm(y), "street"": """)(1), """")(0)
        Cells(y, 5) = Split(Split(itm(y), "suite"": """)(1), """")(0)
        Cells(y, 6) = Split(Split(itm(y), "city"": """)(1), """")(0)
        Cells(y, 7) = Split(Split(itm(y), "zipcode"": """)(1), """")(0)
        Cells(y, 8) = Split(Split(itm(y), "phone"": """)(1), """")(0)
        Cells(y, 9) = Split(Split(itm(y), "website"": """)(1), """")(0)
        Cells(y, 10) = Split(Split(Split(itm(y), "company"": ")(1), "name"": """)(1), """")(0)
        Cells(y, 11) = Split(Split(itm(y), "catchPhrase"": """)(1), """")(0)
        Cells(y, 12) = Split(Split(itm(y), "bs"": """)(1), """")(0)
    Next y
End Sub

Thanks again, Narayan.
 

As in your previous post on exactly same subject
it may be easier with a JSon object than with string operations …
 
@Marc L,
Right you are. I wanted to be more comfortable with this json stuff. That's it. Thanks for your response, by the way.
 
Back
Top