JSON.Simple示例 - 读写JSON
时间:2020-02-23 14:41:30 来源:igfitidea点击:
在本教程中,我们将看到我们如何使用JSON.Simple读写JSON。
JSON.SIMPLE,是一个简单的JAVA库,用于JSON处理,读取和写入JSON数据并完全符合JSON规范(RFC4627)。
在本教程中,我们将使用JSON.Simple读取和写入JSON。
从这里下载json.simple jar。
创建名为"jsonsimpleaexample"的Java项目创建文件夹jar和粘贴下载的jar json-simple-1.1.1.jar。
右键单击项目 - >属性 - > java构建路径 - >添加jar然后转到src-> jars - > json-simple-1.1.1.jar。
单击确定。
然后,我们将看到JSON-Simple-1.1 JAR添加到库中,然后单击确定
将JSON写入文件:
在src-> org.igi.theitroad中创建一个名为"jsonsimplewritingtofileexample.java"的新类
package org.igi.theitroad; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; /* * @Author : igi Mandliya */ public class JSONSimpleWritingToFileExample { public static void main(String[] args) { JSONObject countryObj = new JSONObject(); countryObj.put("Name", "Netherlands"); countryObj.put("Population", new Integer(1000000)); JSONArray listOfStates = new JSONArray(); listOfStates.add("Madhya Pradesh"); listOfStates.add("Maharastra"); listOfStates.add("Rajasthan"); countryObj.put("States", listOfStates); try { //Writing to a file File file=new File("E:CountryJSONFile.json"); file.createNewFile(); FileWriter fileWriter = new FileWriter(file); System.out.println("Writing JSON object to file"); System.out.println("-----------------------"); System.out.print(countryObj); fileWriter.write(countryObj.toJSONString()); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
运行上面的程序,我们将获取以下输出:
Writing JSON object to file --------------------------- {"Name":"Netherlands","Population":1000000,"States":["Madhya Pradesh","Maharastra","Rajasthan"]}
阅读JSON到文件:
其中我们将阅读以上创建的JSON文件。
在src-> org.igi.onitoad中创建一个名为"jsonsimplereadingfromfilexample.java"的新类
package org.igi.theitroad; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /* * @Author : igi Mandliya */ public class JSONSimpleReadingFromFileExample { public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader("E:CountryJSONFile.json")); JSONObject jsonObject = (JSONObject) obj; String nameOfCountry = (String) jsonObject.get("Name"); System.out.println("Name Of Country: "+nameOfCountry); long population = (Long) jsonObject.get("Population"); System.out.println("Population: "+population); System.out.println("States are :"); JSONArray listOfStates = (JSONArray) jsonObject.get("States"); Iterator iterator = listOfStates.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }
运行上面的程序,我们将获取以下输出:
Reading JSON from a file --------------------------- Name Of Country: Netherlands Population: 1000000 States are : Madhya Pradesh Maharastra Rajasthan