gpt4 book ai didi

java - 当我获取 JSON 元素时为空值

转载 作者:行者123 更新时间:2023-12-02 04:22:32 25 4
gpt4 key购买 nike

我正在通过 REST API 自动创建事件。在读取 JSON 元素时,我得到了 null 值,我使用相同的逻辑使用 Rapid7 启动 Scan,并且工作正常。

JSON JSON OBJECT

服务器确实返回具有正确属性的 JSON。但按名称获取属性返回 null。有人能发现这里出了什么问题吗?谢谢

//阅读

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null) {
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(result); // this works fine I can see all JSON elements ( incident_number, Sys_id, state, ect )

if (method.equals("GET")) {
String incident_state = json.get("state").toString();
System.out.print(incident_state);
incident.setState(Integer.parseInt(incident_state));
System.out.print("\n ServiceNow : Successful GET, incident is retrieved" );
}

我在 Rapid7 中使用了相同的逻辑并且工作得很好

//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null) {
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();

JSONParser parser = new JSONParser();
JSONObject json2 = (JSONObject) parser.parse(result);


if(methodType == MethodType.Retrieve_Vulnerability_info) {
String scan_vuln_title= json2.get("title").toString();
String scan_vuln_severityScore = json2.get("severityScore").toString();
String scan_vuln_publishe_date = json2.get("published").toString();
String scan_vuln_descriptiob = json2.get("description").toString() + splunkdata.getNmap_results();
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setSeverityScore(scan_vuln_severityScore);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setVulnerability_title(scan_vuln_title);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setPublished_date(scan_vuln_publishe_date);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setDescription(scan_vuln_descriptiob);
System.out.print("\n Rapid7 : Successful GET, vulnerabilities info of : "+ scan_vuln_title + " were retrieved" );

}

Json 作为字符串:

{
"result": {
"promoted_by": "",
"parent": "",
"caused_by": "",
"watch_list": "",
"upon_reject": "cancel",
"sys_updated_on": "2019-05-17 14:01:56",
"u_validation_contact": "",
"approval_history": "",
"skills": "",
"number": "INC2090774",
"proposed_by": "",
"lessons_learned": "",
"state": "2",
"sys_created_by": "sa_security",
"knowledge": "false",
"order": "",
"cmdb_ci": "",
"u_supplier_reference": "",
"impact": "2",
"active": "true",
"work_notes_list": "",
"priority": "4",
"sys_domain_path": "/",
"u_impacted_services": "",
"business_duration": "",
"group_list": "",
"u_phone_validating": "",
"approval_set": "",
"major_incident_state": "",
"short_description": "test",
"correlation_display": "",
"work_start": "",
"additional_assignee_list": "46cfdce9db7157046ada1ded0b961990,4cff106ddb7157046ada1ded0b961905,2cb1adc2db9d7b803111f2603996197f",
"notify": "2",
"sys_class_name": "incident",
}
}

最佳答案

问题是我必须更深入一步,然后检索结果。 感谢 Austin Schafter 的建议。

  bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(result);
JSONObject JsonResults = (JSONObject) json.get("result"); // here where the change needed
if (method.equals("GET"))
{
String incident_state = JsonResults.get("state").toString(); // then use JSON which store results to retrieve other JSON elements by using name
System.out.print(incident_state);
incident.setState(Integer.parseInt(incident_state));
System.out.print("\n ServiceNow : Successful GET, incident is retrieved" );
}

关于java - 当我获取 JSON 元素时为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56627484/

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