JSON 文件操作
来源:互联网 发布:淘宝设置自动回复内容 编辑:程序博客网 时间:2024/06/05 06:19
一、
我的jar包集(2017):http://pan.baidu.com/s/1c1BigBA
或
commons-beanutils-*
commons-lang-*
commons-collections-*
commons-logging-*
ezmorph-*
json-lib-*
二、提供一份待解析的json文件,test.json(json集合,非集合json文件可以简化)如下:
我的json问件是放在web项目src下的,路径可以根据自己需要更改
[
{
"username":"root",
"truename":"roott",
"password":"paswd"
},
{
"username":"小丽",
"truename":"小毅",
"password":"小英"
},
]
三、Java代码由两个类组成,一个是读取文件内容的Util类,还有一个是主程序Test类。
如果只有一个json{"":""}这种格式,只需要JSONObject jsonObject = jsonArray.getJSONObject(i);就可以了,不需要jsonArray和循环
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Util {
public String ReadFile(String Path) {
BufferedReader reader = null;
String JsonContext = "";
try {
FileInputStream fis = new FileInputStream(Path);
InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String temp = null;
while ((temp = reader.readLine()) != null) {
JsonContext += temp;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return JsonContext;
}
public static void main(String[] args) {
String path = System.getProperty("user.dir") + "/src/config/test.json";
String JsonContext = new Util().ReadFile(path);
JSONArray jsonArray = JSONArray.fromObject(JsonContext);
int size = jsonArray.size();
System.out.println("Size: " + size);
for (int i = 0; i < size; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject);
System.out.println("jsonArray[" + i + "]:username=" + jsonObject.get("username"));
System.out.println("jsonArray[" + i + "]:truename=" + jsonObject.get("truename"));
System.out.println("jsonArray[" + i + "]:password=" + jsonObject.get("password"));
}
}
执行后的结果如下:
jsonArray.Size(): 2
{"username":"root","truename":"roott","password":"paswd"}
jsonArray[0]:username=root
jsonArray[0]:truename=roott
jsonArray[0]:password=paswd
{"username":"小丽","truename":"小毅","password":"小英"}
jsonArray[1]:username=小丽
jsonArray[1]:truename=小毅
jsonArray[1]:password=小英
{"username":"root","truename":"roott","password":"paswd"}
jsonArray[0]:username=root
jsonArray[0]:truename=roott
jsonArray[0]:password=paswd
{"username":"小丽","truename":"小毅","password":"小英"}
jsonArray[1]:username=小丽
jsonArray[1]:truename=小毅
jsonArray[1]:password=小英
阅读全文
0 0
- JSon文件操作
- 文件操作与JSON
- JSON 文件操作
- Python之Json文件操作
- XML文件解析&操作&与JSON比较
- C++ Json 对文件的读写操作
- 10.cocos2d-x文件读写、Json操作
- json文件的解析(读写操作)
- json文件解析为字符串的操作
- 文件操作与数据存储json
- C#读文件,转成Json,写文件操作,例子
- WPF 中对文件操作方式JSON文件,基于C#
- Json 操作
- 操作JSON
- JSON操作
- JSON操作
- json操作
- json 操作
- java基础
- Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- Ubuntu32下执行accpet函数出错
- python日志格式
- Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.5:resources...
- JSON 文件操作
- simditor编辑器,图片上传链接替换
- Java Web 模板代码生成器的设计与实现
- eclipse和Android studio中获取sha1和md5
- caffe 编译中出现的错误——fatal error: hdf5.h: 没有那个文件或目录
- unity引用dll文件方法
- BaiduSpeed百度语音唤醒语音识别语音合成
- web页面优化
- bzoj3853 GCD Array