'label$ is case-sensitive
Input "label : ", l$
'try .server-name or .roles.special.devs or roles.common.regulars.[1]
'JSON Reader in QB64
'By AshishKingdom
Dim As Long startPos
, endPos
, label_pos1
, label_pos2
, num_of_br
Dim As Long f0
, f1
, f2
, f3
, f4
, f5
, f6
, list_obj_pos
Dim As _Byte value_type
, in_quote
'1=object, 2=array, 0=string or number Dim As String sep_type
, curr_label
, ca
, label
, json_data
, tmp_str
label$ = l$
json_data$ = json_data_0$
json_list_sec:
label_pos1
= InStr(label$
, ".") label_pos2
= InStr(label_pos1
+ 1, label$
, ".")
list_pos1
= InStr(label$
, "[") list_pos2
= InStr(list_pos1
+ 1, label$
, "]")
list_index
= Val(Mid$(label$
, list_pos1
+ 1, list_pos2
- list_pos1
- 1)) tmp_str
= _Trim$(json_list
(json_data$
, list_index
)) json_data$ = tmp_str
label$
= Right$(label$
, Len(label$
) - list_pos2
) GoTo json_list_sec
'for nested list (if present), it will keep on parsing it If list_pos1
> label_pos1
And list_pos1
< label_pos2
Then 'list index present
list_pos2
= InStr(list_pos1
+ 1, label$
, "]") list_index
= Val(Mid$(label$
, list_pos1
+ 1, list_pos2
- list_pos1
- 1)) tmp_str
= _Trim$(json_list
(json_data$
, list_index
)) json_data$ = tmp_str
label$
= Right$(label$
, Len(label$
) - list_pos2
) GoTo json_list_sec
'for nested list (if present), it will keep on parsing it
startPos = getAbsCharPos(json_data$, "{", 1)
list_obj_pos = getAbsCharPos(json_data$, "[", 1)
endPos = charFind(json_data$, "}", 1, 1, 1)
f0 = startPos + 1
f2 = getAbsCharPos(json_data$, ":", f0)
f1 = getAbsCharPos(json_data$, ",", f2 + 1)
f5 = getAbsCharPos(json_data$, "[", f0)
f6 = getAbsCharPos(json_data$, "{", f0)
sep_type = ""
f3 = 0
f3 = f6
sep_type = "}"
f3 = f5
sep_type = "]"
f3 = f5
sep_type = "]"
f3 = f6
sep_type = "}"
value_type = 0
'check if ',' was before [ or {
' comma present before [ or {
value_type = 0
'comma present after.. so it may be inside {}/[] or outside of it.
'finding end of position of json_obj/list present
num_of_br = 1
f4 = f3
in_quote = 1
f4 = f4 + 1
ca
= Mid$(json_data$
, f4
, 1) If ca
= "{" Or ca
= "[" Then num_of_br
= num_of_br
+ 1 If ca
= "}" Or ca
= "]" Then num_of_br
= num_of_br
- 1 If sep_type
= "}" Then value_type
= 1 Else value_type
= 2
Print "json_parser_error: ',' before ':'" 'Print value_type, f3, f4
'Sleep
json$
= getData
(Mid$(json_data$
, f2
+ 1, endPos
- f2
- 1)) json$
= getData
(Mid$(json_data$
, f2
+ 1, f1
- f2
- 1)) curr_label
= Mid$(label$
, label_pos1
+ 1, label_pos2
- label_pos1
- 1) 'Print curr_label, getData(Mid$(json_data$, f0, f2 - 1 - f0))
'Sleep
If getData
(Mid$(json_data$
, f0
, f2
- 1 - f0
)) = curr_label
Then json$
= Mid$(json_data$
, f3
, f4
- f3
+ 1) 'Print Mid$(json_data$, f3, f4 - f3 + 1)
'Print Right$(label$, Len(label$) - label_pos2 + 1)
json$
= json$
(Mid$(json_data$
, f3
, f4
- f3
+ 1), Right$(label$
, Len(label$
) - label_pos2
+ 1)) f1 = getAbsCharPos(json_data$, ",", f4 + 1)
'Print "hha ", f1, f0
Dim As Long startPos
, endPos
, curr_index
, json_obj_pos
, value_type
startPos
= InStr(list_data$
, "[") endPos = charFind(list_data$, "]", 0, 1, 1)
json_obj_pos = getAbsCharPos(list_data$, "{", 1)
f0 = startPos + 1
curr_index = 0
f1 = getAbsCharPos(list_data$, ",", f0)
' f2 = getAbsCharPos(list_data$, "[", f0)
f5 = getAbsCharPos(list_data$, "[", f0)
f6 = getAbsCharPos(list_data$, "{", f0)
sep_type = ""
f3 = 0
f3 = f6
sep_type = "}"
f3 = f5
sep_type = "]"
f3 = f5
sep_type = "]"
f3 = f6
sep_type = "}"
value_type = 0
'check if ',' was before [ or {
' comma present before [ or {
value_type = 0
'comma present after.. so it may be inside {}/[] or outside of it.
'finding end of position of json_obj/list present
num_of_br = 1
f4 = f3
in_quote = 1
f4 = f4 + 1
ca
= Mid$(list_data$
, f4
, 1) If ca
= "{" Or ca
= "[" Then num_of_br
= num_of_br
+ 1 If ca
= "}" Or ca
= "]" Then num_of_br
= num_of_br
- 1 f1 = f4 + 1
If sep_type
= "}" Then value_type
= 1 Else value_type
= 2 'Print f0, f1, ca, curr_index, index
'check if there's any value here
element_present = 0
json_list$
= getData
(Mid$(list_data$
, f0
, endPos
- f0
)) json_list$
= Mid$(list_data$
, f0
, endPos
- f0
+ 1) json_list$
= getData
(Mid$(list_data$
, f0
, f1
- f0
)) json_list$
= Mid$(list_data$
, f0
, f1
- f0
) f0 = f1 + 1
curr_index = curr_index + 1
qp = 1
ca$
= Mid$(main_str
, i
, 1) getAbsCharPos~& = i
'Print "yes"
'Print main_str
c$
= Mid$(main_str
, i
, 1) quote$ = c$
c$
= Mid$(main_str
, j
, 1) getData$ = getData$ + c$
reversed$
= reversed$
+ Mid$(main_str
, i
, 1) 'Print reversed$
If ignore_quote
Then charFind~&
= -getAbsCharPos
(reversed$
, search
, p
) + 1 + Len(main_str
) Else charFind~&
= -InStr(p
, reversed$
, search
) + 1 + Len(main_str
) If ignore_quote
Then charFind~&
= getAbsCharPos
(main_str
, search
, p
) Else charFind~&
= InStr(p
, main_str
, search
)