quick-cocos2d-x解析json


先声明下我采用的是http服务器,客户端用的curl。

下面是我服务器端代码,我用的是jetty,其他服务器的都类似:


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);
	}	

}

熟悉json的同学可以看懂我返回的json的数据。

下面是客户端lua代码:

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是上面servlet的响应url。

json.decode()函数会返回一个已经解析好的table也就是上面的变量t。

然后就可以通过t来访问获取到的数据了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值