java 包含大量元素的JSON文件,如何使用java读取和打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12469518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON file with a lot of elements, how to read and print using java?
提问by Yeo
I am very new to JSON. I managed to print out 1 element in the JSON file using Java. However if the file consists of more than 1 element, I do not know how to go about retrieving it. Should I use JSONArray? I tried to search and apply JSONArray, but i got no idea how to. Or does it has something to do with Gson, Hymanson thingy? I got no idea what are they..
我对 JSON 很陌生。我设法使用 Java 打印出 JSON 文件中的 1 个元素。但是,如果文件包含 1 个以上的元素,我不知道如何检索它。我应该使用 JSONArray 吗?我试图搜索和应用 JSONArray,但我不知道如何去做。还是跟 Gson、Hymanson 有关系?我不知道它们是什么..
This is my example.json file:
这是我的 example.json 文件:
{"rel": "/r/AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/ec1914bfb0606c36376fbbcd316e5666022e2469", "features": ["/c/en/apple_inc /r/AtLocation -", "/c/en/apple_inc - /c/en/unite_state", "- /r/AtLocation /c/en/unite_state"], "end": "/c/en/unite_state", "license": "/l/CC/By-SA", "uri": "/a/[/r/AtLocation/,/c/en/apple_inc/,/c/en/unite_state/]", "start": "/c/en/apple_inc", "context": "/ctx/all", "surfaceText": null}
{"rel": "/r/IsA", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/914e6775fd79d660bacf22ec699568e6694da3e8", "features": ["/c/en/america_beautiful /r/IsA -", "/c/en/america_beautiful - /c/en/national_anthem", "- /r/IsA /c/en/national_anthem"], "end": "/c/en/national_anthem", "license": "/l/CC/By-SA", "uri": "/a/[/r/IsA/,/c/en/america_beautiful/,/c/en/national_anthem/]", "start": "/c/en/america_beautiful", "context": "/ctx/all", "surfaceText": null}
This is my java file:
这是我的java文件:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class test
{
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try
{
Object obj = parser.parse(
new FileReader("C:/Users/LijingYeo/Desktop/example.json"));
JSONObject jsonObject = (JSONObject) obj;
String rel = (String) jsonObject.get("rel");
System.out.println(rel);
String start = (String) jsonObject.get("start");
System.out.println(start);
String end = (String) jsonObject.get("end");
System.out.println(end);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
All suggestions and helps rendered are sincerely appreciated by me, thank you for your time and effort!
所有的建议和帮助都得到我的真诚感谢,感谢您的时间和精力!
回答by swemon
I think you should read your json file line by line and then parse to json object. This sample code works for your json file.
我认为您应该逐行读取您的 json 文件,然后解析为 json 对象。此示例代码适用于您的 json 文件。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJsonFile
{
public static void main(String[] args)
{
readJsonFile();
}
public static void readJsonFile() {
BufferedReader br = null;
JSONParser parser = new JSONParser();
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("D:\example.json"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println("Record:\t" + sCurrentLine);
Object obj;
try {
obj = parser.parse(sCurrentLine);
JSONObject jsonObject = (JSONObject) obj;
String rel = (String) jsonObject.get("rel");
System.out.println(rel);
String start = (String) jsonObject.get("start");
System.out.println(start);
String end = (String) jsonObject.get("end");
System.out.println(end);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Json file is yours as below. I hope this may help.
Json 文件是您的,如下所示。我希望这会有所帮助。
回答by Vikdor
Whatever you are doing is right.
无论你做什么都是对的。
Your JSON structure is of type JSONObject
, i.e (Key, Value) pairs. Of those elements, features
element's value is of type JSONArray
. So while browsing through the elements, you should cast the value of features
element to JSONArray
and then iterate through the list of elements in that array.
您的 JSON 结构是类型JSONObject
,即 (Key, Value) 对。在这些元素中,features
元素的值是类型JSONArray
。因此,在浏览元素时,您应该将features
element的值强制转换为JSONArray
,然后遍历该数组中的元素列表。
JSONArray features = (JSONArray) jsonObject.get("features");
int size = features.size();
for (int i = 0; i < size; i++)
{
System.out.println(features.get(i));
}
HTH.
哈。
回答by basiljames
You can use ObjectMapper.
您可以使用ObjectMapper。
String jsonString = null;
if (prettyPrint == true) {
// this is time consuming
jsonString = myObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(myObject);
} else {
// faster option
jsonString = myObjectMapper.writeValueAsString(myObject);
}