Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

No JSON Arrays Left to Parse

$
0
0
I am working to get my code to loop through arrays of JSON strings (of same format) until it reaches the end of the arrays (i.e., no strings left). I need the code to recognize it has reached the end by identifying that a certain identifier (present in every set) does not have additional information in the next array. So I believe I am looking for "while" syntax that says "while this identifier has content" proceed parsing the JSON according to the below code. My existing code works for array of strings for which I know the length - unfortunately the lengths are variable therefore I need flexible syntax to adjust with the lengths (i.e., "For 0 to n" doesn't work every time).

The JSON code I am parsing is in this format:

Code:

{"id":1,"prices":[{"name":"expressTaxi","cost":{"base":"USD4.50"....}}
Here the identifier structure I'd like to augment with some type of while loop (the "i" is the index number depending on the # of loop in the yet to be implemented "while" loop).

Code:

Json("prices")(i)("name")
So ideally looking for something like:

Code:

"While Json("prices")(i)("name") has information" then proceed on....
Please note again, everything works when I know the length -- just looking for a small syntax update, thank you! Full code below:

Code:

Option Explicit

Sub getJSON()
sheetCount = 1
i = 1
urlArray = Array("URL1", “URL2”, “URL3”)

Dim MyRequest As Object: Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim MyUrls: MyUrls = urlArray
Dim k As Long
Dim Json As Object

For k = LBound(MyUrls) To UBound(MyUrls)
    With MyRequest
        .Open "GET", MyUrls(k)
        .Send
        Set Json = JsonConverter.ParseJson(.ResponseText)
      ''[where I’d like some type of While statement checking into the below line for content]
        Sheets("Sheet" & sheetCount).Cells(i, 1) = Json("prices")(i)("name")
        Sheets("Sheet" & sheetCount).Cells(i, 2) = Json("prices")(i)("cost")("base")
        i = i + 1
  End With
sheetCount = sheetCount + 1
Next
End Sub


Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>