本文目录一览:
- 1、json文件可以直接导入数据库吗
- 2、LowDB 轻量级 JSON 本地数据库
- 3、json如何获取封装数据库中的数据
- 4、json数组从数据库获取
- 5、用Json获取到数据库中的值,这段代码中要怎样添加上表格(table))
- 6、如何使用json获得数据库数据
json文件可以直接导入数据库吗
直接读写文件,再把读出来的文件内容格式化成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;
}
将获取到的字符串,入库即可。
LowDB 轻量级 JSON 本地数据库
作为轻量级的本地存储方式,对于构建不依赖服务器的小型项目,用LowDB存储和管理数据是十分理想的选择。在Nodejs, Electron and browser等一些小型项目中经常能看到LowDB的身影。
npm install lowdb
或者:
yarn add lowdb
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync'); // 有多种适配器可选择
const adapter = new FileSync('db.json'); // 申明一个适配器
const db = low(adapter);
db.defaults({posts: [], user: {}, count: 0})
.write();
db.get('posts')
.push({id: 1, title: 'lowdb is awesome'})
.write()
db.set('user.name', 'typicode')
.write()
db.update('count', n = n + 1)
.write()
运行程序会在项目中添加db.json文件,里面存储了添加的数据:
{
"posts": [
{
"id": 1,
"title": "lowdb is awesome"
}
],
"user": {
"name": "typicode"
},
"count": 1
}
lowdb是基于lodash构建的,所以可以使用任何lodash强大的函数,比如: _.get() 和 _.find(),并且可以串联地使用:
db.get('users')
.find({sex: 'male'})
.value()
函数 功能
low(adapter) 返回一个具有特定属性和功能的 lodash chain
db.[...].write() / .value() 写 / 读数据
db.getState() / .setState() 获取 / 设置数据库的状态
db._ 数据库lodash的实例,可以利用这个添加自己的函数或者第三方的mixins,比如lodash-id
db._.mixin({
second: function(array) {
return array[1]
}
})
db.get('posts')
.second()
.value()
针对lowdb自带的适配器:FileSync、FileAsync 和 LocalBrowser,有以下可选参数:
defaultValue: 文件不存在时的默认值;
serialize/deserialize: 写之前和读之后的操作。
const adapter = new FilSync('db.json',{
serialize: (data) = encrypt(JSON.stringify(data)),
deserialize: (data) = JSON.parse(decrypt(data))
})
可以直接使用lodash的函数进行查询。需要注意的是有些操作可能会导致原数据被修改,为了避免这种误操作,需要使用 .cloneDeep(),操作都是惰性的,只有调用 .value()或 .write()后才会正式执行。
检查users是是否存在
db.has('users')
.value()
设置users
db.set('users', [])
.write()
排序、选择
db.get('users')
.filter({sex: 'male'})
.sortBy('age')
.take(5)
.value()
获取特定字段
db.get('users')
.map('name')
.value()
获取数量
db.get('users')
.size()
.value()
获取特定信息
db.get('users[0].name')
.value()
更新信息
db.get('users')
.find({name: 'Tom'})
.assign({name: 'Tim'})
.write()
删除信息
db.get('users')
.remove({name: 'Time'})
.write()
移除属性
db.unset('users.name)
.write()
深拷贝
db.get('users')
.cloneDeep()
.value()
可以使用 shortid 和 lodash-id 为数据库中的每一条记录创建唯一的id索引,然后通过id检索操作记录:
const shortid = require('shortid')
const postId = db
.get('posts')
.push({ id: shortid.generate(), title: 'low!' })
.write()
.id
const post = db
.get('posts')
.find({ id: postId })
.value()
const lodashId = require('lodash-id')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
db._.mixin(lodashId)
// We need to set some default values, if the collection does not exist yet
// We also can store our collection
const collection = db
.defaults({ posts: [] })
.get('posts')
// Insert a new post...
const newPost = collection
.insert({ title: 'low!' })
.write()
// ...and retrieve it using its id
const post = collection
.getById(newPost.id)
.value()
low( ) 函数接受自定义的Adapter
class MyStorage {
constructor() {
// ...
}
read() {
// Should return data (object or array) or a Promise
}
write(data) {
// Should return nothing or a Promise
}
}
const adapter = new MyStorage(args)
const db = low(adapter);
==============================================
英文官网介绍,更加简洁
Lowdb 3 is a pure ESM package. If you're having trouble importing it in your project, please read this.
You can use TypeScript to type check your data.
You can also add lodash or other utility libraries to improve lowdb.
For CLI, server and browser usage, see examples/ directory.
Lowdb has two classes (for asynchronous and synchronous adapters).
Calls adapter.read() and sets db.data .
Note: JSONFile and JSONFileSync adapters will set db.data to null if file doesn't exist.
Calls adapter.write(db.data) .
Holds your db content. If you're using the adapters coming with lowdb, it can be any type supported by JSON.stringify .
For example:
Adapters for reading and writing JSON files.
In-memory adapters. Useful for speeding up unit tests.
Synchronous adapter for window.localStorage .
Adapters for reading and writing text. Useful for creating custom adapters.
If you've published an adapter for lowdb, feel free to create a PR to add it here.
You may want to create an adapter to write db.data to YAML, XML, encrypt data, a remote storage, ...
An adapter is a simple class that just needs to expose two methods:
For example, let's say you have some async storage and want to create an adapter for it:
See src/adapters/ for more examples.
To create an adapter for another format than JSON, you can use TextFile or TextFileSync .
For example:
Lowdb doesn't support Node's cluster module.
If you have large JavaScript objects ( ~10-100MB ) you may hit some performance issues. This is because whenever you call db.write , the whole db.data is serialized using JSON.stringify and written to storage.
Depending on your use case, this can be fine or not. It can be mitigated by doing batch operations and calling db.write only when you need it.
If you plan to scale, it's highly recommended to use databases like PostgreSQL or MongoDB instead.
json如何获取封装数据库中的数据
直接输出就可以了 String jsondata = "{'a':" + a + ",'b':" + b + ",'c':" + c + "}"; 如果abc是字符串就加上单引号,如 String jsondata = "{'a':'" + a + "','b':'" + b + "','c':'" + c + "'}"; 如果abc是数组 String jsondata = "{'a':[" + a[0] + "," + a[1] + "," + a[2] + "],'b':...}"; 如果abc是对象就相对复杂一点 String jsondata = "{'a':{'a_paramStr':'" + a.paramStr + "','a_pramaInt'}:a.pramaInt},'b':...}"; 当然,如果对象的成员变量很多,可以可以通过对象遍历的方式输出,还有包含子对象、子子对象的情况 总结如下 格式:{'数据名':数据值} 数据值为数字时,直接输出 数据值为字符串时,用单引号包含值,'数据值' 数据值为数组时,用中括号包含,[数据值1,数据值2,...] 数据值为对象时,用大括号包含,{'成员名1':成员数值1,'成员名2':{'成员2对象的成员1','成员2对象的成员1值'},...} 数据与数据之间用逗号隔开,以上 一句话,如果传送数据很简单可以自己输出;如果很复杂,乖乖用三方json包吧,我真不懂为什么抗拒别人做的车轮
json数组从数据库获取
直接后台查询数据库,把结果2,11,3,5,66,33,13,65拼接成这种字符串传到前台,前台把js代码改写一下:
var obj = {
title: {
text: '油 耗',
x: -20 //center
},
subtitle: {
text: 'Oil Wear',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'OilWear (L)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: 'L'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: '油耗' //这里去掉逗号
//这里不填写data
}]
});
};
//填充数据
obj.series.data=$.makeArray(result); //result是后台传来的字符串:2,11,3,5,66,33,13,65
//最后渲染图形
$('#container').highcharts(obj);
用Json获取到数据库中的值,这段代码中要怎样添加上表格(table))
json一般是通过js来解析的,然后在页面上显示,所以用Struts-tags标签来遍历肯定是不行的,因为标签所取的值来自于ValeStack,然后输入Ognl表达式得到,这里的Json不符合这个条件。json格式的数据我们是可以通过js代码拆分的,然而我们的table也是可以通过js代码动态生成的,所以你动态生成table的同时把json的数据追加进table里面就可以了。
如何使用json获得数据库数据
/**
*将List转化为json字符串
*@paramlistList
*@returnStringjson格式的字符串
*/
publicstatic T String getString4List(ListT list)
{
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
}