Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Problem with json.decode — Gideros Forum

Problem with json.decode

jalmincejalmince Member
edited June 2014 in General questions
Having issues parsing a json encoded result from a web service, I came down to the following strange result:
running the following :
require("json")
local a = json.encode( { 1, 2, 'fred', {first='mars',second='venus',third='earth'} } )
print( a)
local b = json.decode( a)
print( b)
one would expect the second print to display the initial value !?
but it is not the case ! why ?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    @jalmince and what would be the difference? can you also post the outputs?
  • Here you go !

    Uploading finished.
    [1,2,"fred",{"first":"mars","third":"earth","second":"venus"}]
    table: 07F5BD48

    pls try the 5 lines of code, you should get the same result.
  • kredor7kredor7 Member
    edited June 2014
    That output would be correct. "b" is being returned as a table so you would reference to sections of that like
    print(b[3])
    Would print "fred"
    print(b[4].first)
    Would print "mars"

    To show it all in the console you can use a recursive print function
    function print_r (t, indent, done)
      done = done or {}
      indent = indent or ''
      local nextIndent -- Storage for next indentation value
      for key, value in pairs (t) do
        if type (value) == "table" and not done [value] then
          nextIndent = nextIndent or
              (indent .. string.rep(' ',string.len(tostring (key))+2))
              -- Shortcut conditional allocation
          done [value] = true
          print (indent .. "[" .. tostring (key) .. "] => Table {");
          print  (nextIndent .. "{");
          print_r (value, nextIndent .. string.rep(' ',2), done)
          print  (nextIndent .. "}");
        else
          print  (indent .. "[" .. tostring (key) .. "] => " .. tostring (value).."")
        end
      end
    end
     
    --then do
    print_r(b)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • thanks Kredor7 for that one !
Sign In or Register to comment.