Java 處理 json 數據

  1. 從 HttpServletRequest 獲取 json 值git

    以 http 發送 post或get 請求的方式調用 servlet,如github

    http post http://xxxx/test.do < test.json

    注意:上述的http 爲 *inux 工具,輔助測試。Github官網json

    後端獲取:後端

    Retrieving JSON Object Literal from HttpServletRequestapp

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = request.getReader();
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } finally {
        reader.close();
    }
    System.out.println(sb.toString());
    }
  2. json 格式的字符串轉化爲 Java 中的 json 對象。ide

    jar 包爲 net.sf.json 包 官網工具

    String str = "{ \"data\": \"{a:1,b:2}\" }";
    JSONObject json = (JSONObject)JSONSerializer.toJSON(str);
  3. json 格式的字符串轉化爲 Java 中的 Mappost

    jar 包爲 fastjson測試

    String str = "{ \"data\": \"{a:1,b:2}\" }";
      Map map =  com.alibaba.fastjson.JSON.parseObject(str);

    map 再轉化爲 jsonui

    net.sf.json.JSONObject.JSONObject.fromObject(map).toString()
相關文章
相關標籤/搜索