Parse HttpServletRequest包含jsonp字符串

时间:2012-11-21 07:28:42

标签: java parsing servlets jetty jsonp

当我尝试使用:

发送跨域jsonp请求时
$.getJSON(url + "?callback=?",
    value : 'John',
    record : {
        value : 'a',
        list : [ 1, 2 ]
    });

然后我尝试用这样的java servlet来获取它:

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response)
               throws ServletException, IOException {

        String output = request.getParameter("callback") 
                            + "({\"response\":\"test\"});";

        response.setContentType("application/javascript;charset=utf-8");

        PrintWriter out = response.getWriter();
        out.println(output);
    }
}

内部servlet请求字符串具有参数名称:

_=1353482336546
value=John
record[value]=a

如何将请求字符串解析为原始JSON?

我使用嵌入式jetty服务器,我想在JSON字符串上使用“JSON to Object”jetty解析器

1 个答案:

答案 0 :(得分:0)

您可以应用flexjson将json字符串解析为对象。请看一下:

live example

flexjson library