java json对象转换
引入的jar包:
commons-beanutils-1.9.2.jar
commons-collections-3.2.1.jar
commons-lang-2.6.jar
commons-logging-1.2.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
字符串存json对象里:
String content = "123";
String content2 = "456";
JSONObject jsonObj = new JSONObject();
jsonObj.put("a",content);
jsonObj.put("b",content2);
System.out.println(jsonObj);//输出{"a":"123","b":"456"}
fwriter = new FileWriter(savefile);
fwriter.write(jsonObj.toString()); //把jsonObj转成string类型 存到文件中
fwriter.flush();
fwriter.close();
从json对象里获取字符串
while ((lineTxt = br.readLine()) != null) {
System.out.println(lineTxt); //从文件中读出的内容
JSONObject json = JSONObject.fromObject(lineTxt); //转jsonobject
System.out.println(json.get("I"));//获取I导连
JSONArray arr = (JSONArray) json.get("I");;//获取I导连 放到集合中
//List list = (List) json.get("I"); 同上
System.out.println(list.get(2) instanceof Integer); //true
}