Jump to content

json parsing function


Neutro
 Share

Recommended Posts

Hey guys,

I needed to parse some json. There are already existing solutions using objects if you search in the forum but i wanted to do it the old fashioned way so i thought i'd share it:

 

func find_data_value_in_json($jsonsource, $data_name, $is_text, $occurence = 1, $start_index_keyword = "", $second_index_keyword = "")

    ;json source = the json source where to find the data value
    ;data_name = the name of the data of which we want to find the value
    ;is_text = define if the value of the data is a text or not. Boolean: true or false. It's mandatory regarding how json is formatted.
    ;occurence = (optionnal) define the occurence of the $data_name to find; defaut is 1 (find first occurence)
    ;start_index_keyword = (optionnal) searches this keyword in the json source and start looking for the data from this point.
    ;second_index_keyword = (optionnal) searches this keyword in the json source file from $start_index_keyword and start looking for the data from this point

$index = 1

if $start_index_keyword <> "" Then

    $index = StringInStr($jsonsource, $start_index_keyword, 1, 1)

    if $index = 0 then msgbox("","Error", "Cannot find " & $start_index_keyword & " when looking for " & $data_name & " value")

EndIf

if $second_index_keyword <> "" then

    $index = StringInStr($jsonsource, $second_index_keyword, 1, 1, $index)

    if $index = 0 then msgbox("","Error", "Cannot find " & $second_index_keyword & " when looking for " & $data_name & " value")

EndIf

$index2 = StringInStr($jsonsource, '"' & $data_name & '"', 1, $occurence, $index)

if $index2 = 0 then msgbox("","Error", "Cannot find " & $data_name)


;finding the separator symbol. It can be either "," or "}," or "}"


$index3 = stringinstr($jsonsource, ",", 0, 1, $index2)


if $index3 <> 0 AND stringmid($jsonsource, $index3 - 1, 1) = "}" then $index3 = $index3 - 1

if $index3 = 0 then $index3 = stringinstr($jsonsource, ",", 0, 1, $index2)

if $index3 = 0 then $index3 = stringinstr($jsonsource, "}", 0, 1, $index2)

if $index3 = 0 then msgbox("","Error", "cannot find the separator symbol that ends the value of " & $data_name)



if $is_text = true then ; "data":"value"

    $data_value = stringmid($jsonsource, $index2 + stringlen($data_name) + 4, $index3 - 1 - ($index2 + stringlen($data_name) + 4))

Else ; "data":value

    $data_value = stringmid($jsonsource, $index2 + stringlen($data_name) + 3, $index3 - ($index2 + stringlen($data_name) + 3))

EndIf

return $data_value

EndFunc

Probably not the cleanest / most compliant way to do it but it works for what i needed :)

Enjoy :)

 

Edited by Neutro
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...