forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


JSON Parser

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 6 Source Code
View previous topic :: View next topic  
Author Message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1751
Location: Space and Time

PostPosted: Mon Apr 10, 2017 2:01 pm    Post subject: JSON Parser Reply with quote

This is a very simple JSON parser. I'm sure it could be a lot better but I just wanted something quick. It only works with 1 level. If anyone has a better solution please post. Sorry for lack of commenting. Wink

{"firstName":"Chris","lastName":"Gingerich"}

Code:

  #define function,json
  #define command,json

  #%t = @json(create)
  #json write,%t,name,VDSFreak
  #json write,%t,email,"chris@insuresign.com"
  #info @json(text,%t)
  #info @json(read,%t,email)
  #stop

:json
  #very simple JSON parser
  #%t = @json(create)
  # Creates a JSON ID (a list)
  #%t = @json(read,<JSON ID>,<name>,<default value>)
  #%t = @json(text,<JSON ID>)
  #json loadfile,<JSON ID>,<File name>
  #json write,<JSON ID>,<name>,<value>
  #json savefile,<JSON ID>,<file name>
  if @not(%1)
    error 2
  end
  if @equal(%1,read)
    if @not(%2)
      error 2
    end
    %%_json_text = @text(%2)
    #first try with ,
    %%exit = @strslice(%%_json_text,@chr(34)%3@chr(34):@chr(34),@chr(34)@chr(44))
    if @not(%%exit)
      #then with }
      %%exit = @strslice(%%_json_text,@chr(34)%3@chr(34):@chr(34),@chr(34)"}")
    end
    if @not(%%exit)
      if %4
        %%exit = %4
      end
    end
    #end
    exit %%exit
  elsif @equal(%1,write)
    if @not(%2)
      error 2
    end
    #get the text from the list
    %%_json_text = @trim(@text(%2))
    %p = @pos(@chr(34)%3@chr(34):@chr(34),%%_json_text)
    if @not(@greater(%p,0))
      %c = @pos("}",%%_json_text)
      if @equal(%%_json_text,"{}")
        #if a new JSON string don't add the ,
        %%exit = @strins(%%_json_text,%c,@chr(34)%3@chr(34):@chr(34)%4@chr(34))
      else
        %%exit = @strins(%%_json_text,%c,@chr(44)@chr(34)%3@chr(34):@chr(34)%4@chr(34))
      end
      list clear,%2
      list add,%2,%%exit
      exit
    else
      %l = @len(@chr(34)%3@chr(34):@chr(34)@chr(44))
      %s = @sum(%l,@pred(%p))
      %%p2 = @pred(@pos(@chr(34),%%_json_text,%s))
      %e = @strdel(%%_json_text,%s,%%p2)
      %%exit = @strins(%e,%s,%4)
      list clear,%2
      list add,%2,%%exit
      exit
    end
  elsif @equal(%1,create)
    %l = @new(list)
    list add,%l,"{}"
    exit %l
  elsif @equal(%1,text)
    if @not(%2)
      error 2
    end
    exit @text(%2)
  elsif @equal(%1,savefile)
    if @not(@both(%2,%3))
      error 2
    end
    list savefile,%2,%3
  elsif @equal(%1,loadfile)
    if @not(%2)
      error 2
    end
    list loadfile,%2,%3
    exit
  end
  exit



EDIT: Updated to use a list

_________________
Chris
Http://theblindhouse.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cnodnarb
Professional Member
Professional Member


Joined: 11 Sep 2002
Posts: 763
Location: Rockeledge, GA

PostPosted: Sat Apr 15, 2017 6:04 am    Post subject: Reply with quote

Nice!

Yeah to go to nested levels will be a different beast. I remember struggling with this grabbing inner windows and with the original array function.
Back to top
View user's profile Send private message AIM Address
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1751
Location: Space and Time

PostPosted: Sat Apr 15, 2017 11:06 am    Post subject: Reply with quote

Thanks. Seems like everything outputs to json and I like using this more than an ini file for writing to disk.
_________________
Chris
Http://theblindhouse.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Garrett
Moderator Team


Joined: 04 Oct 2001
Posts: 2149
Location: A House

PostPosted: Sat Apr 15, 2017 5:38 pm    Post subject: Reply with quote

I vaguely remember someone going through this issue when they were trying to work with XML files. I am sure they had a solution to this. This was before VDS offered support for XML files. Try searching the forums for the XML threads.

-Garrett

_________________
'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.)
Back to top
View user's profile Send private message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1751
Location: Space and Time

PostPosted: Mon Apr 17, 2017 8:38 pm    Post subject: Reply with quote

Found it, thanks Garrett.

http://www.vdsworld.com/forum/viewtopic.php?t=2779&highlight=xml+parser

I'll take a look and see if I can use it for JSON

_________________
Chris
Http://theblindhouse.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 6 Source Code All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group