PHP获取JSON的数据可以使用内置的 json_decode() 就可以解码为PHP变量,可根据自己需要的格式来进行格式化并提交到数据库。
例如:
<?php$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'
var_dump(json_decode($json))
var_dump(json_decode($json, true))
?>
将会输出
object(stdClass)#1 (5) {["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
//将记录转为Json格式public <T extends Object>String objectsToJsonStr(List<T>baseModelList, Class beanClass, String datePatten,
int totalCount) {
JSONArray jsonArray = JSONArray.fromObject(baseModelList)
JsonConfig jsonConfig = new JsonConfig()
JSONArray returnJsonArray = new JSONArray()
T baseModel = null
for (int i = 0i <jsonArray.size()i++) {
JSONObject rowData = (JSONObject) jsonArray.get(i)
baseModel = (T) JSONObject.toBean(rowData, beanClass)
JSONObject jo = JSONObject.fromObject(baseModel, jsonConfig)
returnJsonArray.add(jo)
}
StringBuffer jsonString = new StringBuffer()
jsonString.append(returnJsonArray.toString())
// 返回结果集
return jsonString.toString()
}
//使用实例
String strData = objectsToJsonStr(users, S_USER.class, null, users.size())
//users:多条记录S_USER:POJO类
不明白为啥指定json,我的建议是:1、STRUTS文件标签选择并上传EXCEL文件,示例代码:
<html:form action="/uploadExcelFile/UploadExcelFileAction.go" enctype="multipart/form-data" method="post" onsubmit="return confirm_save()">
<table border="0" cellpadding="0" cellspacing="5" align="center" style="margin:50">
<tr>
<td align="right" nowrap >请选择Excel文件(.<%=excelType%>):</td>
<td align="right" colspan="3" valign="middle" nowrap ><html:file property="aircraftExcel" size="200" style="width:400height:25" value=""></html:file></td>
</tr><tr>
<td></td>
<td align="right">Excel文件中标题(表头)占行:</td>
<td align="left"><input name="topicRow" value="0" style="width:50" onKeyUp="value=value.replace(/[\D]/g,'')"/></td>
<TD align="right" nowrap title="上传"><html:submit value="开始更新" style="height:25width:70"></html:submit></TD>
</tr>
</table>
</html:form>
2、UploadExcelFileAction接收、写入后台指定目录(局部代码)
FileOutputStream fos = null
try {
//得到文件数据(数组)
byte[] fileDate = ff.getFileData()
//得到服务器硬盘文件夹的绝对路径
ServletContext application = this.getServlet().getServletContext()
String realPath = application.getRealPath("/")
if(excelType==null||excelType.equals("xlsx"))//默认生成2007以后版本格式
fos = new FileOutputStream(realPath+"aircraftExcelTable.xlsx")
else
fos = new FileOutputStream(realPath+"aircraftExcelTable.xls")
fos.write(fileDate)
fos.close()
if(excelType==null||excelType.equals("xlsx"))//默认生成2007以后版本格式
num=ReadExcelSaveToDB.ReadAndSaveNew(realPath+"aircraftExcelTable.xlsx",topicNumber)
else
num=ReadExcelSaveToDB.ReadAndSave(realPath+"aircraftExcelTable.xls",topicNumber)
if(num==-1){
session.setAttribute("forwardPage", currentPage)
session.setAttribute("prompt", "文件传输失败或无法连接数据库!")
return mapping.findForward("promptpage")
}else if(num==-2){
session.setAttribute("forwardPage", currentPage)
session.setAttribute("prompt", "Excel文件大于1000条或不能识别!重新制作Excel文件。")
return mapping.findForward("promptpage")
}else if(num==-3){
session.setAttribute("forwardPage", currentPage)
session.setAttribute("prompt", "Excel文件中包含非法字符(非数字),请修正后重试。")
return mapping.findForward("promptpage")
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}finally{
try {
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
3、解析读取表数据(POI)并存入数据库(局部代码)
public static int ReadAndSave(String path,String topicNumber){
try {
File file=new File(path)
FileInputStream fs=new FileInputStream(file)
@SuppressWarnings("resource")
HSSFWorkbook workbook=new HSSFWorkbook(fs)
//一共有几张表
int sheet_num=workbook.getNumberOfSheets()
setSheetNumber(0)
setProcess(0)//开始
//循环每个表
for(int i=0i<sheet_numi++){
HSSFSheet sheet=workbook.getSheetAt(i)
setSheetNumber(i+1)
if(sheet!=null&&sheet.getPhysicalNumberOfRows()!=0){
int topicNumberNum=Integer.parseInt(topicNumber)//越过首行的标题(如果有)
totalNumber=sheet.getPhysicalNumberOfRows()-topicNumberNum
//循环各个行
for(int j=sheet.getFirstRowNum()+topicNumberNumj<=sheet.getPhysicalNumberOfRows()j++){
HSSFRow row=sheet.getRow(j)
String aircraft=null
//String aircraftType=null
Double takeoffWeight=0.00
Double payloadMax2=0.00
int seatNumberMax2=0
if(row!=null&&row.getPhysicalNumberOfCells()!=0){
switch(row.getCell(0).getCellType()){
case Cell.CELL_TYPE_STRING:
aircraft=(row.getCell(0).getStringCellValue()).trim()
break
case Cell.CELL_TYPE_NUMERIC:
aircraft=String.valueOf(row.getCell(0).getNumericCellValue()).trim()
break
}
//继续处理其他单元格、行
}
//存入数据库
AircraftTableActionForm atb=new AircraftTableActionForm()
if(aircraft!=null&&!aircraft.equals("")){
atb.setAircraft(aircraft)
//set其他值
updateByExcel(atb)!=-1
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)