gpt4 book ai didi

java - 有没有使用 JSONObject 以 URL 格式传递 Json 数据的方法?

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:10 25 4
gpt4 key购买 nike

我创建了一个 java URL 类,其中包含我的 Json 数据,并具有一些函数来获取我的 json 数据以进行一些数据比较,我发现 JSONObject 可能不支持将数据传递到 JSONObject。在我的例子中,我是否需要使用 JSONArray,因为我的 JSON 数据也具有数组结构?

     try
{
JSONObject obj = new JSONObject ();
obj.readJsonFromUrl(theUrl);
System.out.println(obj.toString());
}

catch(MalformedURLException e)
{
System.out.print("your problem here ...1");
}
}
else
{
System.out.print("Can't Connect");
}



我确信这是给我错误消息的地方,因为它在我的编译器中返回此错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method readJsonFromUrl(URL) is undefined for the type JSONObject



还有一些关于 JSONObject readJsonFromUrl 方法的警告消息

private static JSONObject readJsonFromUrl(URL theUrl) throws IOException, JSONException {

任何人都可以向我解释 JSON 数据在 java 中如何工作?我看到很多 JSON 的 Java 类,这让我很困惑,比如 JSONObject、JSONArray、JSONValue。我在网上搜索了一些信息,但我也不是很清楚,因为我对 JSON 数据处理非常陌生

这是我的示例 json 数据,我需要的数据只是 scan_result

{  
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"file_info":{
"display_name":"",
"file_size":242,
"file_type":"Not available",
"file_type_description":"Not available",
"md5":"aa69ba384f22d0dc0551ace2fbb9ad55",
"sha1":"09ceb54e65df3d3086b222e8643acffe451a6e8a",
"sha256":"dcb46d6ae2a187f789c12f19c44bbe4b9a43bd200a3b306d5e9c1fcf811dc430",
"upload_timestamp":"2016-11-18T09:09:08.390Z"
},
"process_info":{
"blocked_reason":"",
"file_type_skipped_scan":false,
"post_processing":{
"actions_failed":"",
"actions_ran":"",
"converted_destination":"",
"converted_to":"",
"copy_move_destination":""
},
"profile":"File scan",
"progress_percentage":100,
"result":"Allowed",
"user_agent":""
},
"scan_results":{
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"progress_percentage":100,
"scan_all_result_a":"No Threat Detected",
"scan_all_result_i":0,
"scan_details":{
"Ahnlab":{
"def_time":"2016-11-08T15:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":1,
"threat_found":""
},
"Avira":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":133,
"threat_found":""
},
"ClamAV":{
"def_time":"2016-11-08T10:28:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":94,
"threat_found":""
},
"ESET":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":38,
"threat_found":""
}
},
"start_time":"2016-11-18T09:09:08.405Z",
"total_avs":4,
"total_time":250
},
"vulnerability_info":{

}
}

最佳答案

如上所述here ,有很多方法可以解决这个问题。要么你必须自己实现读取、解析操作(@Roland Illig 的回答)

//you have to implement the readJSON method 
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}

或者你可以使用一个库。最知名和最广泛使用的库是 jacksongson 。总体来说,您尝试将 json 对象“映射”到一个类。

您有 json 文件:

{
“id”:1,
“姓名”:“埃里尼”,
"爱好":["音乐","哲学","足球"]
}

以及一个表示该文件并将存储值的类(根据您使用的库,可能有不同的要求,例如 getter、setter 等。)

public class Person {
public int id;
public String name;
public List<String> hobbies = new ArrayList<String>();

public String toString() {
return name +" has the id: " + id + " the following hobbies" + hobbies.get(0) + " " + hobbies.get(2);
}
}

最后在你的主要方法中:

  public static void main(String[] args) throws IOException, ParseException {
ObjectMapper mapper = new ObjectMapper();
InputStream input = this.getClass().getResourceAsStream(FILE); //read your file. There are many ways to achieve this.
ObjectMapper mapper = new ObjectMapper(); // just need one
Person eirini = mapper.readValue(input, Person.class);
System.out.println(eirini.toString());

关于java - 有没有使用 JSONObject 以 URL 格式传递 Json 数据的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40690382/

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