NetTalk Central

Author Topic: Json  (Read 7534 times)

linas@debetas.lt

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
    • Email
Json
« on: January 17, 2012, 04:28:35 PM »
Hi ,

I have a server which gets data form different server using the webclient. So the result I get back is a Json string.. and it can be big .. The idea is to save json in memory table and give it in a nice web browse look.

does anybody made a json converter like this... saving it to queue or file. How easy to transform json to queue/table?

Linas

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Json
« Reply #1 on: January 17, 2012, 06:35:34 PM »
no but I would probably use StringTheory from Capesoft if I had to do the job. One does wonder why the need for another format when XML not only would fit the job but seems much cleaner to read and write.

Flint G

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Json
« Reply #2 on: January 17, 2012, 07:56:48 PM »
Linas,

You can take a look at http://json.org for a description of the protocol.  It's really not that difficult if you are familiar with how to build a state-machine.  Gordon Holfelder has built a utility that will take JSON formatted strings and parse them.  Using getter methods you can extract the exact data you are looking for, and do whatever you want with it.  Gordon's utility is not as auto-magic as Capesoft's xFiles, but it gets the job done.

I actually quite like JSON, and actually prefer it over XML, now.  It tends to lend itself better to low-bandwidth applications, especially mobile platforms, which is why it has gained so much ground so quickly.  You can take a look at an example of JSON in action by watching ClarionLive Webinar #141 at http://www.clarionlive.com/index.php?option=com_content&view=article&id=239:webinar-141-gordon-holfelder-on-saas-extjs-and-nettalk&catid=1:latest-news&Itemid=76, where Gordon showed how he uses NetTalk to send/receive JSON strings to and from a SQL database.  It really won't show you the code that goes into parsing the strings, though.

Hope this helps.

Regards,
Flint
« Last Edit: January 17, 2012, 08:05:38 PM by Flint G »
NetTalk: 12.26
Clarion: 9.1.11529
Brave: 1.31.88
Chrome: 95.0.4638.69
Edge: 95.0.1020.44
ExtJS: 7.0.0.156

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Json
« Reply #3 on: January 17, 2012, 10:48:28 PM »
Hi Linus,

I haven't tried this directly, but using StringTheory it's reasonably easy to parse.

There are two data structures, an "object" and an "array".

Since the array is simpler, let's deal with that first;
(this is all untested, so you may need to work through it. If you find obvious bugs please let me know here)

str   StringTheory
  code
  str.SetValue(incomingJson)
  str.Crop(2,str.Length()-1) ! removes [ and ] from the ends
  str.split(',','"') ! separates the lines. Assumes " used for quotable strings, could be ' though.
 
  now each value in the array is in the str lines queue. See
  http://www.capesoft.com/docs/StringTheory/StringTheoryClasses.htm#SplitJoin
  for accessing this queue.
 
  when I said an object is more complicated, well, I lied.
  It's the exact same code, except that now the name:value pair are in each line of the queue.
  You can use another string theory object to work on these, or just parse them out using INSTRING.
 
  To create a JSON packet you do the reverse;
  a) Use the lines methods to create a collection of entries.
  b) use JOIN to join them all together as a string
  c) use APPEND and PREPEND to add the ] and [ boundaries respectivly.
 
  All in all, no big deal. (which is kinda the point with JSON).

Cheers
Bruce

linas@debetas.lt

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
    • Email
Re: Json
« Reply #4 on: January 18, 2012, 02:32:35 PM »
Thanks,

It appeared that is it very easy

I did split in a rows/lines and then the object extraction and value read I did loops like

loop  ! for object start
 loop ! for value start and end and taking care of extra quotes
 end
end



peace of cake :) and no extra templates just pure string slicing(no StringTheory involved)


Linas

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Json
« Reply #5 on: January 18, 2012, 05:24:56 PM »
You may find using string theory is much faster as it is optimized but really depends how big the data is coming in and if the performance is an issue for you.

BTW String Theory is just a really handy tool to have for all sorts of things and I use it a lot.

Cheers,

Kevin

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Json
« Reply #6 on: January 18, 2012, 09:27:15 PM »
I don't think StringTheory will be faster - the INSTRING in clarion is pretty fast.
StringTheory is just more convenient because the code you end up writing is shorter and cleaner.

Bear in mind that StringTheory is written in Clarion - it's just a string class such as anyone might write. It's definitly a convenience, because it makes lots of everyday chores simpler, but it doesn't (by definition) do anything you can't do in Clarion code yourself, if you want to make the effort.

Aside - most 3rdparty products are like this. Very few contain stuff that's "magical" - most just contain stuff done already so you don't have to. It all boils down to how much you feel your time is worth.

cheers
Bruce

linas@debetas.lt

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
    • Email
Re: Json
« Reply #7 on: January 18, 2012, 09:43:49 PM »
Hi,

I don't use the instring I used the string as array access. String[a:b]  or stirng[a] :)
It is fast... just sometimes I get big Json.. bigger than a megabyte which takes time to download, but not to process.

Linas