【发布时间】:2015-03-29 18:00:24
【问题描述】:
所以我有这么多代码要开始(我包括导入是因为我认为您可能想查看我导入的内容):
import java.sql.Array;
import java.util.Map;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import dataProcessing.Config.Config;
import java.io.*;
public class Reader {
private String file_path;
private FileReader fReader;
public Reader(String filePath) {
this.file_path = filePath;
try {
fReader = new FileReader(file_path);
} catch (Exception e) {
e.printStackTrace();
}
}
public Config read() {
Config c = new Config();
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(fReader);
} catch (Exception e){
//ignore this
}
JSONObject jsonobj = (JSONObject) obj;
if (jsonobj != null) {
c.dailyWorkTime = (Long) jsonobj.get("dailyWorkTime");
c.dburl = (String) jsonobj.get("db_url");
c.username = (String) jsonobj.get("username");
c.password = (String) jsonobj.get("password");
c.millis = (Long) jsonobj.get("millis");
}
return c;
}
}
重要的是,现在我无法在我的 JSON 文件中写入数组。基本上我不能做这样的事情:
{
"database_info": {
"db_url": "hi",
"username": "root",
"password": "root"
},
"system_configuration": {
"dailyWorkTime": 22,
"millis": 600000
},
"entries": {
"organization": [
"orgid","orgname","orgprojects"
],
"store" : [
"stid","stname","st_belong_org"
],
"customers" :[
"phonenumber","facebookid","twitterid","instagramid"
]
}
}
反正其他东西不重要。 我唯一真正需要解析的是“条目”,类似于 String[][] 或 Map。 现在要使用 jsonobj.get() 我必须在我的 json 文件中有直接的条目名称。 任何人都可以帮忙吗? :D 谢谢。
【问题讨论】:
-
您要读取还是写入 JSON文件?
-
我只需要阅读它。