将包含JSON的文件加载到JSONObject中的最简单方法是什么.
目前我正在使用json-lib.
这就是我所拥有的,但它抛出异常:
XMLSerializer xml = new XMLSerializer();
JSON json = xml.readFromFile("samples/sample7.json”); //line 507
System.out.println(json.toString(2));
Run Code Online (Sandbox Code Playgroud)
输出是:
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(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)
Run Code Online (Sandbox Code Playgroud)
jan*_*lle 32
谢谢@Kit Ho的回答.我使用了你的代码,发现我一直遇到错误,我的InputStream总是为null,而在创建JSONObject时则是ClassNotFound异常.这是我的代码版本,它为我提供了一些技巧:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这个答案对于FileInputStream和getResourceAsStream之间的区别很有启发性.希望这也有助于其他人.
Kit*_* Ho 23
试试这个:
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
这是你的sample-json.txt,应该是json格式
{
'foo':'bar',
'coolness':2.0,
'altitude':39000,
'pilot':
{
'firstName':'Buzz',
'lastName':'Aldrin'
},
'mission':'apollo 11'
}
Run Code Online (Sandbox Code Playgroud)
使用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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
131495 次 |
最近记录: |