gad*_*dss 0 lua json coronasdk
嗨,我已经找到了如何在lua中使用post json的教程.
这是代码:
http = require("socket.http")
crypto = require("crypto")
ltn12 = require("ltn12")
url = require("socket.url")
local json = require("json")
local commands_json =
{
["message"] = "Hello",
}
print (commands_json)
local json = {}
json.api_key = "6_192116334"
json.ver = 1
json.commands_json = json.encode(commands_json)
json.commands_hash = crypto.digest(crypto.md5, json.commands_json .. 'hkjhkjhkjh')
local post = "api=" .. url.escape(Json.Encode(json))
local response = {}
local r, c, h = http.request {
url = "http://127.0.0.1/?page=api",
method = "POST",
headers = {
["content-length"] = #post,
["Content-Type"] = "application/x-www-form-urlencoded"
},
source = ltn12.source.string(post),
sink = ltn12.sink.table(response)
}
local path = system.pathForFile("r.txt", system.DocumentsDirectory)
local file = io.open (path, "w")
file:write (Json.Encode(json) .. "\n")
file:write (post .. "\n")
file:write (response[1] .. "\n")
io.close (file)
json = Json.Decode(table.concat(response,''))
native.showAlert("hey", json.commands[1].tot_nbr_rows)
Run Code Online (Sandbox Code Playgroud)
现在我收到了这些错误:
Windows simulator build date: Dec 9 2011 @ 14:01:29
Copyright (C) 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
table: 0346D6D0
Runtime error
...nistrator\my documents\corona projects\json\main.lua:17: attempt to c
all field 'encode' (a nil value)
stack traceback:
[C]: in function 'encode'
...nistrator\my documents\corona projects\json\main.lua:17: in main chun
k
Runtime error: ...nistrator\my documents\corona projects\json\main.lua:17: attem
pt to call field 'encode' (a nil value)
stack traceback:
[C]: in function 'encode'
...nistrator\my documents\corona projects\json\main.lua:17: in main chun
k
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我得到了错误encode.
任何人都可以帮我解决我的情况吗?
提前致谢 ...
这包括外部提供的Json代码,可能带有编码功能:
local json = require("json")
Run Code Online (Sandbox Code Playgroud)
这会丢弃旧的json变量并用空表替换它:
local json = {}
Run Code Online (Sandbox Code Playgroud)
这会尝试调用json.encode现在未定义,因为您重新定义json为上面的空表:
json.commands_json = json.encode(commands_json)
Run Code Online (Sandbox Code Playgroud)
解决方案是选择一个不同的变量名称.
| 归档时间: |
|
| 查看次数: |
1736 次 |
| 最近记录: |