当前位置: 首页 > 工具软件 > JSON lib > 使用案例 >

java利用json-lib解析json字符串

叶坚
2023-12-01

需要jar包:

json-lib-2.3-jdk13.jar

ezmorph-1.0.3.jar

commons-lang.jar

commons-logging.jar

commons-beanutils.jar

xom-1.1.jar

commons-collections-3.2.jar

既然是用json-lib来解析,那么你的项目里自然要加上这个包了,我只分享代码:

        String qr = request.getParameter("queryparams");//queryparams是页面拼好的json数组

//queryparams是页面拼好的json数组如下:

               {"params":[{"field0":"NAME","operateflag0":"@contain#","searchcontant0":"北","logic0":"and"},   {"field1":"ID","operateflag1":"@contain#","searchcontant1":"1","logic1":"or"},{"field2":"SEQUENCE","operateflag2":"@contain#","searchcontant2":"5"}]}
         System.out.println("接收到的字符串:" + qr);
        //将json字符串转成json对象
        JSONObject jb = JSONObject.fromObject(qr);
        //取查询参数params,是个json数组
        JSONArray jsons = jb.getJSONArray("params");
        int jsonLength = jsons.size();
             //对json数组进行循环

        for (int i = 0; i < jsonLength; i++) {
            JSONObject tempJson = JSONObject.fromObject(jsons.get(i));
            System.out.println("ddd:" + tempJson);
            String field = StringEscapeUtils.escapeSql(tempJson.getString("field" + i));
                      String operateFlag = StringEscapeUtils.escapeSql(tempJson.getString("operateflag" + i));
                     String searchContant = StringEscapeUtils.escapeSql(tempJson.getString("searchcontant" + i));
                     String logic = StringEscapeUtils.escapeSql(tempJson.getString("logic" + i));
             }

基本完成,ajax用json比较方便

 类似资料: