First declare that I use the HTTP server and the curl used by the client.
Below is my server code, I use jetty, and other servers are similar:
public class JsonServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("application/json");resp.setCharacterEncoding("UTF-8");Map map = new HashMap(); map.put( "name", "json" ); map.put( "bool", Boolean.TRUE ); map.put( "int", new Integer(1) ); map.put( "arr", new String[]{"a","b"} ); String jsonStr=JSONArray.fromObject(map).toString(); resp.getWriter().println(jsonStr); resp.flushBuffer(); System.out.println(jsonStr);}}
Those who are familiar with JSON can understand the JSON data returned by me.
The following is the client Lua code:
local function callback(event)local ok = (event.name == "completed")local request = event.requestlocal response = request:getResponseString() print(response)local json=require("framework.shared.json")local t=json.decode(response)print(t)endlocal request = network.createHTTPRequest(callback, "http://localhost:8080/json", "POST")request:start()
Http: // ....../JSON is the response URL of the servlet above.
The JSON. Decode () function returns a parsed table, that is, the preceding variable t.
Then you can access the obtained data through T.