{"id":1024,"date":"2022-08-30T15:11:07","date_gmt":"2022-08-30T15:11:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/error-in-json-in-corona-lua-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:07","modified_gmt":"2022-08-30T15:11:07","slug":"error-in-json-in-corona-lua-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/error-in-json-in-corona-lua-collection-of-common-programming-errors\/","title":{"rendered":"error in json in corona lua-Collection of common programming errors"},"content":{"rendered":"<p>hi i have found a tutorial on how to use post json in lua.<\/p>\n<p>here is the code :<\/p>\n<pre><code>http = require(\"socket.http\")\ncrypto = require(\"crypto\")\nltn12 = require(\"ltn12\")\nurl = require(\"socket.url\")\nlocal json = require(\"json\")\n\nlocal commands_json = \n{\n\n                [\"message\"] = \"Hello\",\n\n}\nprint (commands_json)\nlocal json = {}\njson.api_key = \"6_192116334\"\njson.ver = 1\njson.commands_json = json.encode(commands_json)\njson.commands_hash = crypto.digest(crypto.md5, json.commands_json .. 'hkjhkjhkjh')\n\nlocal post = \"api=\" .. url.escape(Json.Encode(json))\nlocal response = {}\n\nlocal r, c, h = http.request {\n    url = \"http:\/\/127.0.0.1\/?page=api\",\n    method = \"POST\",\n    headers = {\n        [\"content-length\"] = #post,\n        [\"Content-Type\"] =  \"application\/x-www-form-urlencoded\"\n    },\n    source = ltn12.source.string(post),\n    sink = ltn12.sink.table(response)\n}\n\nlocal path = system.pathForFile(\"r.txt\", system.DocumentsDirectory)\nlocal file = io.open (path, \"w\")\nfile:write (Json.Encode(json) .. \"\\n\")\nfile:write (post .. \"\\n\")\nfile:write (response[1] .. \"\\n\")\nio.close (file)\n\njson = Json.Decode(table.concat(response,''))\nnative.showAlert(\"hey\", json.commands[1].tot_nbr_rows)\n<\/code><\/pre>\n<p>now i got these error:<\/p>\n<pre><code>Windows simulator build date: Dec  9 2011 @ 14:01:29\n\n\nCopyright (C) 2009-2011  A n s c a ,  I n c .\n        Version: 2.0.0\n        Build: 2011.704\ntable: 0346D6D0\nRuntime error\n        ...nistrator\\my documents\\corona projects\\json\\main.lua:17: attempt to c\nall field 'encode' (a nil value)\nstack traceback:\n        [C]: in function 'encode'\n        ...nistrator\\my documents\\corona projects\\json\\main.lua:17: in main chun\nk\nRuntime error: ...nistrator\\my documents\\corona projects\\json\\main.lua:17: attem\npt to call field 'encode' (a nil value)\nstack traceback:\n        [C]: in function 'encode'\n        ...nistrator\\my documents\\corona projects\\json\\main.lua:17: in main chun\nk\n<\/code><\/pre>\n<p>i don&#8217;t know why i got the error from <code>encode<\/code>.<\/p>\n<p>can anyone can help me about my case?<\/p>\n<p>thanks in advance &#8230;<\/p>\n<ol>\n<li>\n<p>This includes the Json code provided externally, likely with an encode function:<\/p>\n<pre><code>local json = require(\"json\")\n<\/code><\/pre>\n<p>This throws away your old <code>json<\/code> variable and replaces it with an empty table:<\/p>\n<pre><code>local json = {}\n<\/code><\/pre>\n<p>And this tries to call <code>json.encode<\/code> which is now undefined since you redefined <code>json<\/code> as an empty table above:<\/p>\n<pre><code>json.commands_json = json.encode(commands_json)\n<\/code><\/pre>\n<p>The solution is to pick a different variable name.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:15:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>hi i have found a tutorial on how to use post json in lua. here is the code : http = require(&#8220;socket.http&#8221;) crypto = require(&#8220;crypto&#8221;) ltn12 = require(&#8220;ltn12&#8221;) url = require(&#8220;socket.url&#8221;) local json = require(&#8220;json&#8221;) local commands_json = { [&#8220;message&#8221;] = &#8220;Hello&#8221;, } print (commands_json) local json = {} json.api_key = &#8220;6_192116334&#8221; json.ver = 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1024","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1024"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1024\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}