先读取json文件,读出的文件流以JSONObject.fromObject()转换为键值对,再以jsonObject.getString(key)读取键值
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
public class JsonTest{
public static String readFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
FileInputStream input = new FileInputStream(jsonFile);
Reader reader = new InputStreamReader(input, "UTF-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return jsonStr;
}
public static void main(String[] args) {
String readFile=readFile("\\chatluck.json");
JSONObject jsonObject = JSONObject.fromObject(readFile);
String get_url =jsonObject.getString("url");
}
}