gpt4 book ai didi

java - JSON文件元素很多,如何用java读取和打印?

转载 作者:行者123 更新时间:2023-12-05 03:15:45 24 4
gpt4 key购买 nike

我是 JSON 的新手。我设法使用 Java 打印出 JSON 文件中的 1 个元素。但是,如果文件包含超过 1 个元素,我不知道如何去检索它。我应该使用 JSONArray 吗?我尝试搜索并应用 JSONArray,但我不知道该怎么做。或者它与 Gson、Jackson 之类的东西有关吗?我不知道它们是什么..

这是我的 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}

这是我的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();
}

}

}

我衷心感谢您提供的所有建议和帮助,感谢您的时间和努力!

最佳答案

我认为您应该逐行读取您的 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 文件是你的,如下所示。我希望这可能有所帮助。

关于java - JSON文件元素很多,如何用java读取和打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com