我需要使用以下结构读取Java中的JSON文件:
{"id_user":"10","level":"medium","text":"hello 10"}
{"id_user":"20","level":"medium","text":"hello 20"}
{"id_user":"30","level":"medium","text":"hello 30"}
Run Code Online (Sandbox Code Playgroud)
谢谢!.
[POST-EDITED]
我有这个代码,但只读取第一个JSON对象,我需要逐个读取这三个对象.
private void loadJSONFile(){
FileReader fileReader = new FileReader(pathFile);
try (JsonReader jsonReader = new JsonReader(fileReader)) {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("filter_level")) {
System.out.println(jsonReader.nextString());
} else if (name.equals("text")) {
System.out.println("text: " + jsonReader.nextString());
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
jsonReader.close();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
wha*_*ger 11
这是一个基于(并经过测试)的工作示例,使用gson-2.8.0。它接受给定输入流上的任意JSON 对象序列。而且,当然,它不会对您格式化输入的方式施加任何限制:
InputStream is = /* whatever */
Reader r = new InputStreamReader(is, "UTF-8");
Gson gson = new GsonBuilder().create();
JsonStreamParser p = new JsonStreamParser(r);
while (p.hasNext()) {
JsonElement e = p.next();
if (e.isJsonObject()) {
Map m = gson.fromJson(e, Map.class);
/* do something useful with JSON object .. */
}
/* handle other JSON data structures */
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我知道这个帖子已经差不多一年:)但我实际上再次作为答案重新发布,因为我有这个问题和你一样
我有这个text.txt文件 - 我知道这不是一个有效的Json数组 - 但如果你看一下,你会发现这个文件的每一行都是一个Json对象.
{"Sensor_ID":"874233","Date":"Apr 29,2016 08:49:58 Info Log1"}
{"Sensor_ID":"34234","Date":"Apr 29,2016 08:49:58 Info Log12"}
{"Sensor_ID":"56785","Date":"Apr 29,2016 08:49:58 Info Log13"}
{"Sensor_ID":"235657","Date":"Apr 29,2016 08:49:58 Info Log14"}
{"Sensor_ID":"568678","Date":"Apr 29,2016 08:49:58 Info Log15"}
Run Code Online (Sandbox Code Playgroud)
现在我想阅读上面的每一行,并将名称"Sensor_ID"和"Date"解析成Json格式.经过长时间的搜索,我有以下几点:
试一试,然后在控制台上查看结果.我希望它有所帮助.
package reading_file;
import java.io.*;
import java.util.ArrayList;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class file_read {
public static void main(String [] args) {
ArrayList<JSONObject> json=new ArrayList<JSONObject>();
JSONObject obj;
// The name of the file to open.
String fileName = "C:\\Users\\aawad\\workspace\\kura_juno\\data_logger\\log\\Apr_28_2016\\test.txt ";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
obj = (JSONObject) new JSONParser().parse(line);
json.add(obj);
System.out.println((String)obj.get("Sensor_ID")+":"+
(String)obj.get("Date"));
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道读取 JSON 的两个选项。
JSON Simple 适用于小型 JSON 结果。但 GSON 对于大 json 结果非常有用。因为你可以在GSON中设置Object形式。
第一个json.jar
用法 :
String st = ""; // your json object to string
JSONObject newJson = null;
try {
newJson = new JSONObject(st);
newJson.getJSONObject("key");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
第二个gson.jar
用法:
int one = gson.fromJson("1", int.class);
Integer one = gson.fromJson("1", Integer.class);
Long one = gson.fromJson("1", Long.class);
Boolean false = gson.fromJson("false", Boolean.class);
String str = gson.fromJson("\"abc\"", String.class);
String anotherStr = gson.fromJson("[\"abc\"]", String.class);
Run Code Online (Sandbox Code Playgroud)