使用Java读取多个对象JSON [英] Read Multiple Objects JSON with Java
问题描述
我需要使用以下结构读取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}
谢谢!
[已编辑]
我有这段代码,但只能读取第一个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();
}
}
谢谢! <解决方案
我知道这已经差不多一年了:)但是我实际上是在重新发布一个答案,因为我遇到了和你一样的问题
我有这个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}
现在我想读取上面的每一行,并将名称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;
//要打开的文件的名称。
String fileName =C:\\ Users\\aawad\\workspace\\kura_juno\\data_logger\\\\\\\\\\\\\\\\\\\\\\\ Apr_28_2016\\ 。文本 ;
//这将一次引用一行
String line = null;
尝试{
// FileReader以默认编码读取文本文件。
FileReader fileReader = new FileReader(fileName);
//总是将FileReader包装在BufferedReader中。
BufferedReader bufferedReader = new BufferedReader(fileReader); $($)
$ b 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));
}
//总是关闭文件。
bufferedReader.close();
}
catch(FileNotFoundException ex){
System.out.println(Unable to open file'+ fileName +');
}
catch(IOException ex){
System.out.println(Error reading file'+ fileName +');
//或者我们可以这样做:
// ex.printStackTrace();
} catch(ParseException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
I need to read a JSON file in Java with the following structure:
{"id_user":"10","level":"medium","text":"hello 10"}
{"id_user":"20","level":"medium","text":"hello 20"}
{"id_user":"30","level":"medium","text":"hello 30"}
Thanks!.
[POST-EDITED]
I have this code but only read the first JSON Object, I need read the three objects one by one.
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();
}
}
thanks!
I know it has been almost one year for this post :) but i am actually reposing again as an answer because i had this problem same as you Yuan
I have this text.txt file - I know this is not a valid Json array - but if you look, you will see that each line of this file is a Json object in its case alone.
{"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"}
Now I want to read each line of the above and parse the names "Sensor_ID" and "Date" into Json format. After long search, I have the following:
Try it and look on the console to see the result. I hope it helps.
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();
}
}
}
这篇关于使用Java读取多个对象JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!