Package net.sf.json

Examples of net.sf.json.JSONArray


    }


    public static JSONArray getJSONArray(JSONObject obj, String key, JSONArray... default_value) {
        try {
            JSONArray object = obj.getJSONArray(key);
            if (object == null) return default_value.length == 0 ? null : default_value[0];
            return object;
        } catch (Exception e) {
            return default_value.length == 0 ? null : default_value[0];
        }
View Full Code Here


        mockJSON.expectedMessageCount(1);
        mockJSON.message(0).body().isInstanceOf(byte[].class);

        Object json = template.requestBody("direct:marshalInlineOptionsArray", "<ar><el>1</el><el>2</el><el>3</el><el>4</el></ar>");
        String jsonString = context.getTypeConverter().convertTo(String.class, json);
        JSONArray array = (JSONArray) JSONSerializer.toJSON(jsonString);
        assertTrue("Expected a JSON array with string elements: 1, 2, 3, 4", array.containsAll(Arrays.asList("1", "2", "3", "4")));
        mockJSON.assertIsSatisfied();
    }
View Full Code Here

    } else if (jsonTxt.startsWith("[")) {
      jsonTxt = "{datasets: " + jsonTxt + "}";
    }
   
    JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);
    JSONArray  sets = json.getJSONArray("datasets");
   
    Datasets dsets = new Datasets();
    for (int i=0; i < sets.size(); i++) {
      JSONObject set = sets.getJSONObject(i);
      GwtJsonDataset s = new GwtJsonDataset(set);
      Dataset d = datasetReader.createDatasetFromJson(s);
      dsets.add(d);
    }
   
View Full Code Here

    logger.info("Sending: "+command.getClass().getSimpleName()+": "+command);
    String response = put(command.toString());
    logger.info("Received: "+response.trim()+" after "+command.getClass().getSimpleName());
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setExcludes(new String[]{"cookie", "length"});
    final JSONArray jsonResponse = (JSONArray) JSONSerializer.toJSON(response,jsonConfig);
    return signalEvents(jsonResponse);
  }
View Full Code Here

   * @param params
   * @return
   */
  public static JSONArray getList(String sql, Object... params) {
    Connection con=DBPool.getConnection();
    final JSONArray jsonArr = new JSONArray();
    try {
      QueryRunner runner = new QueryRunner();
      runner.query(con,sql.toString(), new ResultSetHandler<Object>() {
        public Object handle(ResultSet rs) throws SQLException {
          ResultSetMetaData metaData = rs.getMetaData();
          int cols = metaData.getColumnCount();
          while (rs.next()) {
            JSONObject jsonObj = new JSONObject();
            for (int i = 1; i <= cols; i++) {
              String colName = metaData.getColumnLabel(i);
              String value = rs.getString(colName);
              if (value != null && value.length() != 0) {
                jsonObj.put(colName.toLowerCase(), value);// 列名全部转换成小写,便于前台代码统一处理
              }
            }
            jsonArr.add(jsonObj);
          }
          return jsonArr;
        }
      }, params);
    } catch (Exception e) {
View Full Code Here

   * @param sql
   * @param params
   * @return
   */
  public static JSONObject getSingle(String sql, Object... params) {
    JSONArray result = getList(sql, params);
    if (result == null || result.size() == 0) {
      return null;
    }
    return result.getJSONObject(0);
  }
View Full Code Here

    }
   
    //取指定板块的最新30帖,带分页,根据页码pageIndex和每页帖子数postPerPage计算
    int start=(pageIndex-1)*postPerPage;
    int end=pageIndex*postPerPage;
    JSONArray arr=CommonDAO.getList(SqlMgr.getSql("GET_30_POSTS"),blockId,blockId,blockId,start,end);
    this.setReqAttr("posts30",arr.toString());
    this.setSessionAttr("blockId", blockId);
   
    //取当前版块的总页数
    JSONObject obj=CommonDAO.getSingle(SqlMgr.getSql("GET_POST_COUNT"), this.blockId);
    if(obj!=null){
View Full Code Here

  public String readPost(){
    //读取主帖和跟帖,包括每个帖子对应的用户信息;每页20条,postId为主帖ID,每页显示20个帖子
    int ct=20;
    int start=(pageIndex-1)*ct;
    int end=pageIndex*ct;
    JSONArray arr=CommonDAO.getList(SqlMgr.getSql("READ_POST"),postId,postId,start,end);
    System.out.println(arr.toString());
   
    //获取跟帖总页数
    JSONObject obj=CommonDAO.getSingle(SqlMgr.getSql("GET_COMMENT_COUNT"),postId,postId);
    if(obj!=null){
      String temp=obj.getString("total");
      if(!StringUtil.isEmpty(temp)){
        this.totalPages=(int) Math.ceil(Float.parseFloat(temp)/ct);
        this.setReqAttr("totalPages", totalPages);
      }
    }
   
    //阅读数+1
    CommonDAO.update(SqlMgr.getSql("READ_PLUS"), postId);
    //主帖用户金币数增加0.01
    CommonDAO.update(SqlMgr.getSql("ADD_USER_MONEY"),0.01,getSessionAttr("userName"));
   
    this.setReqAttr("posts",arr.toString());
    return "success";
  }
View Full Code Here

   * @return
   */
  private String generateBlockTree(){
    StringBuffer result=new StringBuffer();
    result.append("<ul id='navigation'>");//注意:此ID和页面脚本绑定,不要更改
    JSONArray roots=CommonDAO.getList(SqlMgr.getSql("GET_FORUM_CHILD_BLOCK"),0);
    if(roots!=null){
      for(int i=0;i<roots.size();i++){
        JSONObject jsonObj=roots.getJSONObject(i);
        String bId=jsonObj.getString("block_id");
        String link=jsonObj.getString("link");
        if(i==0){
          this.blockId=jsonObj.getString("block_id");
        }
        if(link.equals("1")){
          result.append("<li><a id='"+bId+"' href='loadPosts30.action?blockId="+bId+"'>");
        }else{
          result.append("<li><a id='"+bId+"' href='#'>");
        }
       
        result.append(jsonObj.get("block_name"));
        result.append("</a>");
        String blockId=jsonObj.getString("block_id");
       
        JSONArray children=CommonDAO.getList(SqlMgr.getSql("GET_FORUM_CHILD_BLOCK"), blockId);
        if(children!=null&&children.size()!=0){
          result.append("<ul>");
          for(int j=0;j<children.size();j++){
            JSONObject child=children.getJSONObject(j);
            bId=child.getString("block_id");
            link=child.getString("link");
            if(link.equals("1")){
              result.append("<li><a id='"+bId+"' href='loadPosts30.action?blockId="+bId+"'>");
            }else{
View Full Code Here

  /**
   * 取论坛版块
   * @return
   */
  private void getForumBlocks(){
    JSONArray roots=CommonDAO.getList(SqlMgr.getSql("GET_FORUM_ROOT_BLOCK"));
    this.setSessionAttr("rootBlocks", roots.toString());
   
    JSONObject chObj=new JSONObject();
    for(int i=0;i<roots.size();i++){
      JSONObject obj=roots.getJSONObject(i);
      String bId=obj.getString("block_id");
      JSONArray children=CommonDAO.getList(SqlMgr.getSql("GET_FORUM_CHILD_BLOCK"),bId);
      chObj.put(bId, children.toString());
    }
    this.setSessionAttr("cBlockMap", chObj.toString());
  }
View Full Code Here

TOP

Related Classes of net.sf.json.JSONArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.