分别采用jsonArray下的getString(index)、getLong(index)、getDouble(index)、getInt(index)等方法。
同样,如果要获取java数组中的元素,只需要遍历该数组。
Java代码 收藏代码
/**
* 将json数组转化为Long型
* @param str
* @return
*/
public static Long[] getJsonToLongArray(String str) {
JSONArray jsonArray = JSONArray.fromObject(str)
Long[] arr=new Long[jsonArray.size()]
for(int i=0i<jsonArray.size()i++){
arr[i]=jsonArray.getLong(i)
System.out.println(arr[i])
}
return arr
}
/**
* 将json数组转化为String型
* @param str
* @return
*/
public static String[] getJsonToStringArray(String str) {
JSONArray jsonArray = JSONArray.fromObject(str)
String[] arr=new String[jsonArray.size()]
for(int i=0i<jsonArray.size()i++){
arr[i]=jsonArray.getString(i)
System.out.println(arr[i])
}
return arr
}
/**
* 将json数组转化为Double型
* @param str
* @return
*/
public static Double[] getJsonToDoubleArray(String str) {
JSONArray jsonArray = JSONArray.fromObject(str)
Double[] arr=new Double[jsonArray.size()]
for(int i=0i<jsonArray.size()i++){
arr[i]=jsonArray.getDouble(i)
}
return arr
}
/**
* 将json数组转化为Date型
* @param str
* @return
*/
public static Date[] getJsonToDateArray(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString)
Date[] dateArray = new Date[jsonArray.size()]
String dateString
Date date
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd")
for (int i = 0i <jsonArray.size()i++) {
dateString = jsonArray.getString(i)
try {
date=sdf.parse(dateString)
dateArray[i] = date
} catch (Exception e) {
e.printStackTrace()
}
}
return dateArray
}
public static void main(String[] args) {
JSONArray jsonLongs = new JSONArray()
jsonLongs.add(0, "111")
jsonLongs.add(1, "222.25")
jsonLongs.add(2, new Long(333))
jsonLongs.add(3, 444)
Long[] log=getJsonToLongArray(jsonLongs.toString())
for(int i=0i<log.lengthi++){
System.out.println(log[i])
}
JSONArray jsonStrs = new JSONArray()
jsonStrs.add(0, "2011-01-01")
jsonStrs.add(1, "2011-01-03")
jsonStrs.add(2, "2011-01-04 11:11:11")
Date[] d=getJsonToDateArray(jsonStrs.toString())
for(int i=0i<d.lengthi++){
System.out.println(d[i])
}
}
/*结果如下:
* 111
* 222
* 333
* 444
*
* Sat Jan 01 00:00:00 CST 2011
* Mon Jan 03 00:00:00 CST 2011
* Tue Jan 04 00:00:00 CST 2011
*/
java解析json格式文件,再保存在数据库的方法:1:定义一个实体类
2:用json lib将json字符串转为Java对象
3:用jdbc或hibernate将java对象存入数据库
直接读写文件,再把读出来的文件内容格式化成json,再用JDBC、Mybatis或者其他框架将json数据存入数据库。
假设实体类是这样的:
public class ElectSet {public String xueqi
public String xuenian
public String startTime
public String endTime
public int menshu
public String isReadDB
//{"xueqi":,"xuenian":,"startTime":,"endTime":,"renshu":,"isReadDB":}
public String getXueqi() {
return xueqi
}
public void setXueqi(String xueqi) {
this.xueqi = xueqi
}
public String getXuenian() {
return xuenian
}
public void setXuenian(String xuenian) {
this.xuenian = xuenian
}
public String getStartTime() {
return startTime
}
public void setStartTime(String startTime) {
this.startTime = startTime
}
public String getEndTime() {
return endTime
}
public void setEndTime(String endTime) {
this.endTime = endTime
}
public int getMenshu() {
return menshu
}
public void setMenshu(int menshu) {
this.menshu = menshu
}
public String getIsReadDB() {
return isReadDB
}
public void setIsReadDB(String isReadDB) {
this.isReadDB = isReadDB
}
}
有一个json格式的文件,存的信息如下:
Sets.json:
{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}
具体 *** 作:
/*
* 取出文件内容,填充对象
*/
public ElectSet findElectSet(String path){
ElectSet electset=new ElectSet()
String sets=ReadFile(path)//获得json文件的内容
JSONObject jo=JSONObject.fromObject(sets)//格式化成json对象
//System.out.println("------------" jo)
//String name = jo.getString("xuenian")
//System.out.println(name)
electset.setXueqi(jo.getString("xueqi"))
electset.setXuenian(jo.getString("xuenian"))
electset.setStartTime(jo.getString("startTime"))
electset.setEndTime(jo.getString("endTime"))
electset.setMenshu(jo.getInt("menshu"))
electset.setIsReadDB(jo.getString("isReadDB"))
return electset
}
//设置属性,并保存
public boolean setElect(String path,String sets){
try {
writeFile(path,sets)
return true
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
return false
}
}
//读文件,返回字符串
public String ReadFile(String path){
File file = new File(path)
BufferedReader reader = null
String laststr = ""
try {
//System.out.println("以行为单位读取文件内容,一次读一整行:")
reader = new BufferedReader(new FileReader(file))
String tempString = null
int line = 1
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//显示行号
System.out.println("line " line ": " tempString)
laststr = laststr tempString
line
}
reader.close()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (reader != null) {
try {
reader.close()
} catch (IOException e1) {
}
}
}
return laststr
}
将获取到的字符串,入库即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)