将包含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.<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)
答案 0 :(得分:31)
谢谢@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);
}
}
}
我发现这个答案对于difference between FileInputStream and getResourceAsStream具有启发性。希望这也有助于其他人。
答案 1 :(得分: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 );
}
}
这是你的sample-json.txt,应该是json格式
{
'foo':'bar',
'coolness':2.0,
'altitude':39000,
'pilot':
{
'firstName':'Buzz',
'lastName':'Aldrin'
},
'mission':'apollo 11'
}
答案 2 :(得分:8)
使用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
}
}
答案 3 :(得分:3)
另一种方法是使用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字符串后获得的对象。
答案 4 :(得分:0)
在 Google'e Gson 库中,用于拥有JsonObject
或更抽象的JsonElement
:
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
JsonElement json = JsonParser.parseReader( new InputStreamReader(new FileInputStream("/someDir/someFile.json"), "UTF-8") );
这不需要用于接收/读取json字符串的给定对象结构。
答案 5 :(得分:0)
我正在使用的 json 示例:
"identity" : {
"presentProvince" : [ {
"language" : "eng",
"value" : "China"
} ],
"presentAddressLine1" : [ {
"language" : "eng",
"value" : "bjbjbhjbhj"
} ],
"permanentAddressLine4" : [ {
"language" : "eng",
"value" : "123456"
} ]
} }
这是访问语言和值的代码:
public static void JsonObjParsing() throws Exception {
File f = new File("id.json");
if (f.exists()){
InputStream is = new FileInputStream(f);
String jsonTxt = IOUtils.toString(is, "UTF-8");
System.out.println(jsonTxt);
JSONObject json = new JSONObject(jsonTxt);
JSONObject identity = json.getJSONObject("identity");
JSONArray identityitems = identity.getJSONArray("presentProvince");
for (Object o : identityitems) {
JSONObject jsonLineItem = (JSONObject) o;
String language = jsonLineItem.getString("language");
String value = jsonLineItem.getString("value");
System.out.println(language +" " + value);
}
}
}
public static void main(String[] args) throws Exception {
JsonObjParsing();
}