I loop the txt file with the tipical Open FileName For Input As #fileNum.
now i need to get the value between number and street, and store each value in MyvarNum (As String)
in my case:
4
5
50
11
20
...
48/c
ecc
The file contents look like JSON, so you are probably better treating it as such rather than trying to parse it out manually. If you look in the codebank forum on here I am sure there are one or two JSON parsers available with examples on how to use them.
The file contents look like JSON, so you are probably better treating it as such rather than trying to parse it out manually. If you look in the codebank forum on here I am sure there are one or two JSON parsers available with examples on how to use them.
tks. bro.
but not found a good solution in codebank.
Can you post a direct link to read this json by path?
As this file may NOT be JSON, you can do something like this:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim sStreetNumber As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9, 6)
j = InStr(1, sStreetNumber, """")
sStreetNumber = Mid(sStreetNumber, 1, j - 1)
Debug.Print sStreetNumber 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
Now, if a street 'number' happens to be longer than 4 characters, you can do an Instr() search on "street" as well....like this:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim sStreetNumber As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9)
j = InStr(1, sStreetNumber, "street")
sStreetNumber = Mid(sStreetNumber, 1, j - 4)
Debug.Print sStreetNumber 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
Last edited by SamOscarBrown; May 23rd, 2022 at 07:48 AM.
As this file may NOT be JSON, you can do something like this:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim sStreetNumber As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9, 6)
j = InStr(1, sStreetNumber, """")
sStreetNumber = Mid(sStreetNumber, 1, j - 1)
Debug.Print sStreetNumber 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
Now, if a street 'number' happens to be longer than 4 characters, you can do an Instr() search on "street" as well....like this:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim sStreetNumber As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9)
j = InStr(1, sStreetNumber, "street")
sStreetNumber = Mid(sStreetNumber, 1, j - 4)
Debug.Print sStreetNumber 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
GREAT!
I can use the same techinque for street?
for example to have:
SP75
Via Dieci Giornate
Via Dante Alighieri
....
ecc
It doesn't appear to be a legal JSON document, but instead a series of anonymous JSON objects that were jammed into one file.
You'll probably have to parse it into separate documents to load into a JSON DOM object one by one, find a JSON parser that will generate an outer array wrapping these objects, modify an existing parser to do that, or roll your own custom parser for just this file format.
It is hard to imagine anyone would have spewed this data as it is. Have you created this yourself out of something else, perhaps stripping off an outer JSON array?
Well...having brain farts right now, so instead of using arrays, here's an example of how to add the street name:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim sStreetNumber As String
Dim sStreetName As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
'find street number
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9)
j = InStr(1, sStreetNumber, "street")
sStreetNumber = Mid(sStreetNumber, 1, j - 4)
'find streetn name
k = InStr(1, sString, "unit")
sStreetName = Mid(sString, 1, k - 4)
m = InStrRev(sStreetName, ":")
sStreetName = Mid(sStreetName, m + 2)
Debug.Print sStreetNumber & " " & sStreetName 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
Don't forget that this is pidgin-JSON. Not legal JSON but it probably still follows the syntax otherwise.
As such you must deal with whitespace (blanks, LF, even CR and TAB), each object's properties can be in any order, may use escaping for string text, etc.
Hacking together a specific parser might well work today and then when the originator makes a small change BLAM your program begins to fail.
Well...having brain farts right now, so instead of using arrays, here's an example of how to add the street name:
Code:
Private Sub Command1_Click()
Dim sString As String
Dim fileNum As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim sStreetNumber As String
Dim sStreetName As String
fileNum = FreeFile
Open App.Path & "\Test.txt" For Input As #fileNum
Do While Not EOF(fileNum)
Line Input #fileNum, sString
'find street number
i = InStr(1, sString, "number")
sStreetNumber = Mid(sString, i + 9)
j = InStr(1, sStreetNumber, "street")
sStreetNumber = Mid(sStreetNumber, 1, j - 4)
'find streetn name
k = InStr(1, sString, "unit")
sStreetName = Mid(sString, 1, k - 4)
m = InStrRev(sStreetName, ":")
sStreetName = Mid(sStreetName, m + 2)
Debug.Print sStreetNumber & " " & sStreetName 'or put into a listbox or whatever
Loop
Close fileNum
End Sub
No prob...when you get the opportunity, look at how to use Instr() and Instrrev() functions. Also, this file can be put into a two dimensional array, separating ALL of the information as one might need---then just pull from the array as desired.
Oh that file is definitely JSON. It's not "well formed" or as dilettante said, legal JSON but it's JSON nonetheless. Such files really should be parsed using the proper tools. Using ad-hoc methods can break down if the format changes even slightly. Real parsers are written specifically to be intelligent enough to handle all nuances of a particular format.
For example, look at the following:-
Code:
Dim s As String
Code:
DiM s AS _
String
The VB6 compiler's parser can parse both forms of the above code. Ad-hoc parsers typically would not be able to handle the second one. It's the same with any document with strict and complicated formatting rules like JSON and XML. Ad-hoc parsing is just begging the universe for problems later down the line.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Well, as it may be....I have NO idea if that 'file' is a one-time shot or not. If not, then, yeah...probably my 'solution' is not the appropriate one. But then, who knows what or where the file came from, or if it is going to be 'downloaded' (with different information) in the future. OP usually doesn't share all pertinent information.
If you have control over the file's precise format then you can get away with simple parsing. Nothing wrong with that, just don't cry if the wheels come off your wagon later.
I looked at my JsonBag class and changing it to accept multiple calls to extract such "stacked JSON" was pretty simple. I just added an Optional ByRef Offset parameter to its JSON property. Then looping through is simple:
Code:
Dim F As Integer
Dim SourceData As String
Dim Offset As Long 'Initially 0.
F = FreeFile(0)
Open "Test.txt" For Input As #F
SourceData = Input$(LOF(F), #F)
Close #F
With New JsonBag
Do
.JSON(Offset) = SourceData
If Offset < 0 Then Exit Do
Debug.Print .Item("properties").Item("number")
Loop
End With
Well I would need to update the documentation which I haven't done yet. That needs a little time because I made other changes between version 2.6 and this new 2.7 with that Offset parameter.
And I need to do a lot more testing to fix any bugs the changes may have introduced.
Then I would have to delete some of my attachments here, since I'm at my limit and the moderators sneered when I asked for an increase.
You could always hack a version 2.6 for just this by making small changes though:
Code:
Public Property Get JSON(Optional ByRef Offset As Long = 0) As String
CursorOut = 1
SerializeItem vbNullString, Me
JSON = Left$(TextOut, CursorOut - 1)
'Clear for next call. Do it here to reclaim space.
TextOut = vbNullString
End Property
Public Property Let JSON(Optional ByRef Offset As Long = 0, ByRef RHS As String)
Clear
CursorIn = Offset + 1
LengthIn = Len(RHS)
SkipWhitespace RHS
If Offset <> 0 Then 'We are processing stacked JSON documents beyond the 1st document.
If CursorIn > LengthIn Then
Offset = -1 'No more stacked JSON documents.
Exit Property
End If
End If
Select Case Mid$(RHS, CursorIn, 1)
Case LBRACE
CursorIn = CursorIn + 1
mIsArray = False
ParseObject RHS, CursorIn, LengthIn
Case LBRACKET
CursorIn = CursorIn + 1
mIsArray = True
ParseArray RHS, CursorIn, LengthIn
Case Else
Error99A0 "either " & LBRACE & " or " & LBRACKET, CursorIn
End Select
Offset = CursorIn - 1
End Property
I think that making those RED changes to version 2.6 are all that you would need.
Edit:
Already found a bug. Look for the BLUE "- 1" added near the end of those changes.
CursorIn is a 1-based value and Offset is 0-based, so that adjustment is required there.