Sending Strings and Extracting Information

Hi so I have a string with chunks of information in it.  I’m trying to decode the string and extract the information.

This is how I have been doing it, and I know its not the best way.

Here’s the string before:

[lua]

appWarpClient.sendUpdatePeers(

who…"/"

…“what”…"#"

…player1.y…"+"

…vy…"-"

)[/lua]

Here’s how I’ve been decoding it:

[lua]

function onUpdatePeersReceived(update)

    who = string.sub(update, 0, string.find(update, “/”)-1)

    

    what = string.sub(update, string.find(update, “/”)+1, string.find(update, “#”)-1)

    

    where = string.sub(update, string.find(update, “#”)+1, string.find(update, “+”)-1)

    where = tonumber(where)

    _vy = string.sub(update, string.find(update, “+”)+1, string.find(update, “-”)-1)

    _vy = tonumber(_vy)

end[/lua]

Does anyone know a cleaner way of doing this because I am quickly running out of symbols that will work :slight_smile:

Magic word is JSON :) http://docs.coronalabs.com/daily/api/library/json/index.html

Paste this into main.lua and tinker with it:

[lua]

– Data as it comes from from server

local dataAsAString = ‘{“who”: “me”, “what”: “what in the …”, “player1”: “George”}’

– Now decode

local json = require “json”

local dataDecoded = json.decode( dataAsAString )

– Find the who and what

print(dataDecoded[“who”])

print(dataDecoded[“what”])

[/lua]

Perfect thank you very much jonjonsson!  So much Simpler.

So this is off topic but the mention of JSON has got me thinking about my next step.  Could you direct me to some good sources for learning how to encrypt save files?

I just posted in this thread how to encrypt and decrypt a string: http://forums.coronalabs.com/topic/45113-sending-strings-and-extracting-information/

It requires a pro account though. Not sure what is the best way to do this for free.

Magic word is JSON :) http://docs.coronalabs.com/daily/api/library/json/index.html

Paste this into main.lua and tinker with it:

[lua]

– Data as it comes from from server

local dataAsAString = ‘{“who”: “me”, “what”: “what in the …”, “player1”: “George”}’

– Now decode

local json = require “json”

local dataDecoded = json.decode( dataAsAString )

– Find the who and what

print(dataDecoded[“who”])

print(dataDecoded[“what”])

[/lua]

Perfect thank you very much jonjonsson!  So much Simpler.

So this is off topic but the mention of JSON has got me thinking about my next step.  Could you direct me to some good sources for learning how to encrypt save files?

I just posted in this thread how to encrypt and decrypt a string: http://forums.coronalabs.com/topic/45113-sending-strings-and-extracting-information/

It requires a pro account though. Not sure what is the best way to do this for free.