当我获取JSON元素时为空值

时间:2019-06-17 08:27:03

标签: java json api

我正在通过REST API自动创建事件。在读取JSON元素时,我得到了空值,我使用相同的逻辑从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",
}
}

1 个答案:

答案 0 :(得分:0)

问题是我必须再深入一步,然后获取结果。  感谢Austin Schafter的建议。

RawInline