这是给出的PHP代码
<?php
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
$sTable = "ajax";
$gaSql['user'] = "";
$gaSql['password'] = "";
$gaSql['db'] = "";
$gaSql['server'] = "localhost";
include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS id, ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
// Add the row ID and class to the object
$row['DT_RowId'] = 'row_'.$aRow['id'];
$row['DT_RowClass'] = 'grade'.$aRow['grade'];
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>
我需要在java中开发一个像PHP代码一样的程序(我无法理解PHP代码), 这是我的java代码(部分)。
public class DTSerializedObject implements java.io.Serializable
{
public int iTotalRecords;
public int iTotalDisplayRecords;
public ArrayList<Object> aaData;
}
在函数内部
ArrayList<Object> jsonarray = new ArrayList<Object>();
DTSerializedObject dts = new DTSerializedObject();
dts.iTotalRecords = dts.iTotalDisplayRecords = 30;
String category=null;
while(rs.next())
{
category="Normal";
ArrayList<Object> ja = new ArrayList<Object>();
int topicno=rs.getInt("topicno");
ja.add(topicno);
ja.add(rs.getString("filename"));
ja.add(rs.getString("letterno"));
ja.add(rs.getString("date"));
if((rs.getString("category")).equals("2"))
category="Urgent";
ja.add("<center>"+category+"</center>");
ja.add(rs.getString("office"));
ja.add(rs.getString("subject")+"....");
ja.add("<center><a class='anc' href=\"/Spandana2/specifiedTopicDetailsAndReplies.do?topicno="+topicno+"&purpose=view&jsessionid="+java.util.UUID.randomUUID().toString().replace("-","").toUpperCase()+"\">View</a></center>");
jsonarray.add(ja);
}
dts.aaData = jsonarray;
如何在我的java代码中添加DT_RowId,DT_RowClass选项。 上述PHP代码的预期JSON输出格式是什么?
提前致谢
答案 0 :(得分:2)
像这样设置一个对象
class Bean{
public String col1;
public String col2;
public String DT_RowClass;
}
从db获取数据时创建了一个对象(Bean),并为每个行的对象属性赋值。
ArrayList al = new ArrayList();
while(rs.next){
Bean b = new Bean();
b.col1(rs.getString(1));
b.col2(rs.getString(2));
if(rs.getString(3))
b.DT_RowClass = "class1";
else
b.DT_RowClass = "class2";
al.add(b);
}
创建了一个类,它表示在网页中显示数据所需的json格式。 创建该类的对象( obj )并将适当的值分配给 iTotalRecords &amp; iTotalDisplayRecords &amp; aaData = al ;
public class DTSerializedObject implements java.io.Serializable
{
public int iTotalRecords;
public int iTotalDisplayRecords;
public ArrayList<Object> aaData;
}
最后使用flexjson序列化该对象。
String result = new flexjson.JSONSerializer().exclude("class").serialize(obj);
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-store");
response.getWriter().print(result);