Quick-cocos2d-x parsing json

Source: Internet
Author: User

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:

 


[Java] public class JsonServlet extends HttpServlet {
 
Private static final long serialVersionUID = 1L;
 
@ Override
Protected 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 );
}
 
}

Public class JsonServlet extends HttpServlet {

Private static final long serialVersionUID = 1L;

@ Override
Protected 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 );
}

} Anyone familiar with json can understand the json data returned by me.

The following is the client lua code:


[Html] local function callback (event)
Local OK = (event. name = "completed ")
Local request = event. request
Local response = request: getResponseString ()
Print (response)
Local json = require ("framework. shared. json ")
Local t = json. decode (response)
Print (t)
End
 
Local request = network. createHTTPRequest (callback, "http: // localhost: 8080/json", "POST ")
Request: start ()

Local function callback (event)
Local OK = (event. name = "completed ")
Local request = event. request
Local response = request: getResponseString ()
Print (response)
Local json = require ("framework. shared. json ")
Local t = json. decode (response)
Print (t)
End

Local 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.