diff --git a/README.md b/README.md index 9890719..5979fe0 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,17 @@ print(json.encode({ 1, 2, 'fred', {first='mars',second='venus',third='earth'} }) json = require("json") testString = [[ { "one":1 , "two":2, "primes":[2,3,5,7] } ]] decoded = json.decode(testString) -table.foreach(decoded, print) +for key, item pairs(decoded) do + print(key, item) +end print ("Primes are:") -table.foreach(o.primes,print) +for key, item ipairs(decoded.primes) do + print(key, item) +end ``` ``` one 1 two 2 -primes table: 0032B928 Primes are: 1 2 2 3 diff --git a/examples/example.lua b/examples/example.lua index 36497da..b503fa9 100755 --- a/examples/example.lua +++ b/examples/example.lua @@ -18,6 +18,10 @@ print('JSON encoded test is: ' .. jsonTest) result = json.decode(jsonTest) print ("The decoded table result:") -table.foreach(result,print) +for key, item in pairs(result) do + print(key, item) +end print ("The decoded table result.three") -table.foreach(result.three, print) +for i, item in ipairs(result.three) do + print(i, item) +end diff --git a/examples/tests.lua b/examples/tests.lua index 999bec3..130aba3 100755 --- a/examples/tests.lua +++ b/examples/tests.lua @@ -185,7 +185,7 @@ function testJSON4Lua() -- Test json.null management t = { 1,2,json.null,4 } assert( json.encode(t)=="[1,2,null,4]" ) - t = {x=json.null } + t = {x=null } r = json.encode(t) assert( json.encode(t) == '{"x":null}' ) diff --git a/json/json.lua b/json/json.lua index 9ab4abd..c2d5ba0 100755 --- a/json/json.lua +++ b/json/json.lua @@ -137,7 +137,7 @@ function json.decode(s, startPos) return decode_scanString(s,startPos) end if string.sub(s,startPos,startPos+1)=='/*' then - return decode(s, decode_scanComment(s,startPos)) + return json.decode(s, decode_scanComment(s,startPos)) end -- Otherwise, it must be a constant return decode_scanConstant(s,startPos) @@ -161,6 +161,7 @@ end -- @param startPos The starting position for the scan. -- @return table, int The scanned array as a table, and the position of the next character to scan. function decode_scanArray(s,startPos) + local object = {} local array = {} -- The return value local stringLen = string.len(s) assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s ) @@ -414,4 +415,4 @@ function isEncodable(o) return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null) end -return json \ No newline at end of file +return json