解释一下json文件(W3school)
JSON 语法是 JavaScript 语法的子集。
JSON 语法规则
JSON 语法是 JavaScript 对象表示法语法的子集。
- 数据在名称/值对中
- 数据由逗号分隔
- 花括号保存对象
- 方括号保存数组
JSON 名称/值对
JSON 数据的书写格式是:名称/值对。
名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:
"firstName" : "John"
这很容易理解,等价于这条 JavaScript 语句:
firstName = "John"
JSON 值
JSON 值可以是:
- 数字(整数或浮点数)
- 字符串(在双引号中)
- 逻辑值(true 或 false)
- 数组(在方括号中)
- 对象(在花括号中)
- null
JSON 对象
JSON 对象在花括号中书写:
对象可以包含多个名称/值对:
{ "firstName":"John" , "lastName":"Doe" }
这一点也容易理解,与这条 JavaScript 语句等价:
firstName = "John"
lastName = "Doe"
JSON 数组
JSON 数组在方括号中书写:
数组可包含多个对象:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。
JSON 使用 JavaScript 语法
因为 JSON 使用 JavaScript 语法,所以无需额外的软件就能处理 JavaScript 中的 JSON。
通过 JavaScript,您可以创建一个对象数组,并像这样进行赋值:
例子
var employees = [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName": "Carter" }
];
可以像这样访问 JavaScript 对象数组中的第一项:
employees[0].lastName;
返回的内容是:
Gates
可以像这样修改数据:
employees[0].lastName = "Jobs";
在下面的章节,您将学到如何把 JSON 文本转换为 JavaScript 对象。
JSON 文件
- JSON 文件的文件类型是 ".json"
- JSON 文本的 MIME 类型是 "application/json"
参考链接:https://www.w3school.com.cn/json/json_syntax.asp
Java将excel文件转换为json格式
但是目前还有错误,还没有解决。
需要的jar包
jar包下载地址
net.sf.json jar.zip:https://download.csdn.net/download/yql_617540298/12502605
poi-4.1.2.jar:https://download.csdn.net/download/yql_617540298/12502720
poi-3.9.jar:http://www.java2s.com/Code/Jar/p/Downloadpoi39sourcesjar.htm
poi-ooxml-4.1.2:https://downloads.apache.org/poi/release/maven/poi-ooxml/
excel格式:
excel文件路径:G:\eclipse-workspace\test1\test.xlsx
package test1;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* excel表格转成json
* @ClassName: Excel2JSONHelper
* @Description:TODO(这里用一句话描述这个类的作用)
* @date 2017年1月6日 下午4:42:43
*/
public class Excel2JSONHelper {
//常亮,用作第一种模板类型,如下图
private static final int HEADER_VALUE_TYPE_Z=1;
//第二种模板类型,如下图
private static final int HEADER_VALUE_TYPE_S=2;
public static void main(String[] args) {
File dir = new File("G:\\eclipse-workspace\\test1\\test.xlsx");
Excel2JSONHelper excelHelper = getExcel2JSONHelper();
//dir文件,0代表是第一行为保存到数据库或者实体类的表头,一般为英文的字符串,2代表是第二种模板,
JSONArray jsonArray = excelHelper.readExcle(dir, 2, 2);
System.out.println(jsonArray.toString());;
}
/**
*
* 获取一个实例
*/
private static Excel2JSONHelper getExcel2JSONHelper(){
return new Excel2JSONHelper();
}
/**
* 文件过滤
* @Title: fileNameFileter
* @Description: TODO(这里用一句话描述这个方法的作用)
* @date 2017年1月6日 下午4:45:42
* @return: void
* @throws
*/
private boolean fileNameFileter(File file){
boolean endsWith = false;
if(file != null){
String fileName = file.getName();
endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
}
return endsWith;
}
/**
* 获取表头行
* @Title: getHeaderRow
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param sheet
* @param: @param index
* @param: @return
* @date 2017年1月6日 下午5:05:24
* @return: Row
* @throws
*/
private Row getHeaderRow(Sheet sheet, int index){
Row headerRow = null;
if(sheet!=null){
headerRow = sheet.getRow(index);
}
return headerRow;
}
/**
* 获取表格中单元格的value
* @Title: getCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param row
* @param: @param cellIndex
* @param: @param formula
* @param: @return
* @date 2017年1月6日 下午5:40:28
* @return: Object
* @throws
*/
private Object getCellValue(Row row,int cellIndex,FormulaEvaluator formula){
Cell cell = row.getCell(cellIndex);
if(cell != null){
switch (cell.getCellType()) {
//String类型
case STRING:
return cell.getRichStringCellValue().getString();
//number类型
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().getTime();
} else {
return cell.getNumericCellValue();
}
//boolean类型
case BOOLEAN:
return cell.getBooleanCellValue();
//公式
case FORMULA:
return formula.evaluate(cell).getNumberValue();
default:
return null;
}
}
return null;
}
/**
* 获取表头value
* @Title: getHeaderCellValue
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param headerRow
* @param: @param cellIndex 英文表头所在的行,从0开始计算哦
* @param: @param type 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @param: @return
* @date 2017年1月6日 下午6:12:21
* @return: String
* @throws
*/
private String getHeaderCellValue(Row headerRow,int cellIndex,int type){
Cell cell = headerRow.getCell(cellIndex);
String headerValue = null;
if(cell != null){
//第一种模板类型
if(type == HEADER_VALUE_TYPE_Z){
headerValue = cell.getRichStringCellValue().getString();
int l_bracket = headerValue.indexOf("(");
int r_bracket = headerValue.indexOf(")");
if(l_bracket == -1){
l_bracket = headerValue.indexOf("(");
}
if(r_bracket == -1){
r_bracket = headerValue.indexOf(")");
}
headerValue = headerValue.substring(l_bracket+1, r_bracket);
}else if(type == HEADER_VALUE_TYPE_S){
//第二种模板类型
headerValue = cell.getRichStringCellValue().getString();
}
}
return headerValue;
}
/**
* 读取excel表格
* @Title: readExcle
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param file
* @param: @param headerIndex
* @param: @param headType 表头的类型第一种 姓名(name)英文于实体类或者数据库中的变量一致
* @return: void
* @throws
*/
public JSONArray readExcle(File file,int headerIndex,int headType){
List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
if(!fileNameFileter(file)){
return null;
}else{
try {
//加载excel表格
WorkbookFactory wbFactory = new WorkbookFactory();
Workbook wb = wbFactory.create(file);
//读取第一个sheet页
Sheet sheet = wb.getSheetAt(0);
//读取表头行
Row headerRow = getHeaderRow(sheet, headerIndex);
//读取数据
FormulaEvaluator formula = wb.getCreationHelper().createFormulaEvaluator();
for(int r = headerIndex+1; r<= sheet.getLastRowNum();r++){
Row dataRow = sheet.getRow(r);
Map<String, Object> map = new HashMap<String, Object>();
for(int h = 0; h<dataRow.getLastCellNum();h++){
//表头为key
String key = getHeaderCellValue(headerRow,h,headType);
//数据为value
Object value = getCellValue(dataRow, h, formula);
if(!key.equals("") && !key.equals("null") && key != null ){
map.put(key, value);
}
}
lists.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSONArray jsonArray = JSONArray.fromObject(lists);
return jsonArray;
}
}
错误就是:
尝试了好多方法,都没有解决。好忧桑。
参考链接:https://blog.csdn.net/allen202/article/details/54145479
参考链接:https://blog.csdn.net/konglongaa/article/details/78510342
JAVA入门之如何通过 POI 读取并修改 Excel
http://jingyan.baidu.com/article/fdbd4277cbaccab89e3f48a3.html
POI读取Excel常见问题
http://www.cnblogs.com/qingxinblog/articles/3647483.html
POI操作Excel常用方法总结
http://blog.csdn.net/educast/article/details/50454810