从json文本文件加载JSONObject的最佳方法是什么?

将包含JSON的文件加载到JSONObject中的最简单方法是什么。

目前我正在使用json-lib。

这就是我所拥有的,但它引发了一个例外:

XMLSerializer xml = new XMLSerializer(); JSON json = xml.readFromFile("samples/sample7.json”); //line 507 System.out.println(json.toString(2)); 

输出是:

 Exception in thread "main" java.lang.NullPointerException at java.io.Reader.(Reader.java:61) at java.io.InputStreamReader.(InputStreamReader.java:55) at net.sf.json.xml.XMLSerializer.readFromStream(XMLSerializer.java:386) at net.sf.json.xml.XMLSerializer.readFromFile(XMLSerializer.java:370) at corebus.test.deprecated.TestMain.main(TestMain.java:507) 

尝试这个:

 import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import org.apache.commons.io.IOUtils; public class JsonParsing { public static void main(String[] args) throws Exception { InputStream is = JsonParsing.class.getResourceAsStream( "sample-json.txt"); String jsonTxt = IOUtils.toString( is ); JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt ); double coolness = json.getDouble( "coolness" ); int altitude = json.getInt( "altitude" ); JSONObject pilot = json.getJSONObject("pilot"); String firstName = pilot.getString("firstName"); String lastName = pilot.getString("lastName"); System.out.println( "Coolness: " + coolness ); System.out.println( "Altitude: " + altitude ); System.out.println( "Pilot: " + lastName ); } } 

这是你的sample-json.txt,应该是json格式

 { 'foo':'bar', 'coolness':2.0, 'altitude':39000, 'pilot': { 'firstName':'Buzz', 'lastName':'Aldrin' }, 'mission':'apollo 11' } 

谢谢@Kit Ho的回答。 我使用了你的代码,发现我一直遇到错误,我的InputStream总是为null,而在创建JSONObject时则是ClassNotFoundexception。 这是我的代码版本,它为我提供了一些技巧:

 import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.json.JSONObject; public class JSONParsing { public static void main(String[] args) throws Exception { File f = new File("file.json"); if (f.exists()){ InputStream is = new FileInputStream("file.json"); String jsonTxt = IOUtils.toString(is, "UTF-8"); System.out.println(jsonTxt); JSONObject json = new JSONObject(jsonTxt); String a = json.getString("1000"); System.out.println(a); } } } 

我发现这个答案对于FileInputStream和getResourceAsStream之间的区别很有启发性。 希望这也有助于其他人。

使用java 8,您可以尝试这样:

 import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class JSONUtil { public static JSONObject parseJSONFile(String filename) throws JSONException, IOException { String content = new String(Files.readAllBytes(Paths.get(filename))); return new JSONObject(content); } public static void main(String[] args) throws IOException, JSONException { String filename = "path/to/file/abc.json"; JSONObject jsonObject = parseJSONFile(filename); //do anything you want with jsonObject } } 

另一种做同样的方法可能是使用Gson Class

 String filename = "path/to/file/abc.json"; Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(filename)); SampleClass data = gson.fromJson(reader, SampleClass.class); 

这将给出在解析json字符串之后获得的对象。