从特定过滤器获取信息

时间:2019-07-02 13:30:21

标签: javascript json

由于JSON,我正在解析一些信息

当我做Json.projectStatus.status :时,我有两个响应(“错误”或“确定”)

我只想显示错误信息

function search(projectKey, projectName, lastAnalysis) {
    var url = "https://***?projectKey=" + projectKey;
    request(
        {
            url : url,
            headers : {
                "Authorization" : auth
            }
        },
        function (error, response, body) {
            if(error){
                console.log(error);
            } else {
                process(body, projectName, lastAnalysis);
            }
        }
    );
}

function process(response, projectName, lastAnalysis){
    var Json = JSON.parse(response);
    var color = "#6f6f68";
    var description = "";
    if (Json.projectStatus.status == "ERROR") {
        color = "#FA4643";
        description = ", Errors: ";
        var Data = Json.projectStatus.conditions.map(function(status) {

            if (status.actualValue > 0) {
                description = description 
                              + status.actualValue 
                              + " " + status.metricKey 
                              + ", ";
            }
        });
    }
    var project = {
        name: projectName, 
        status: Json.projectStatus.status, 
        color:color, 
        lastAnalysis:lastAnalysis, 
        description
    };
    Projects.push(project);
    counter++;
    if (counter == numberProjects) {
        parse();
    }
}

function parse(){
    Projects.sort(function(a, b) {
        return a.lastAnalysis - b.lastAnalysis;
    });
    Projects.reverse();
    writer();
}

JSON响应:

    {"projectStatus":{"status":"ERROR","conditions":[{"status":"OK","metricKey":"coverage","comparator":"LT","errorThreshold":"40","actualValue":"63.1"},{"status":"ERROR","metricKey":"duplicated_lines_density","comparator":"GT","errorThreshold":"10","actualValue":"12.3"},{"status":"ERROR","metricKey":"blocker_violations","comparator":"GT","errorThreshold":"0","actualValue":"3"},{"status":"OK","metricKey":"bugs","comparator":"GT","errorThreshold":"5","actualValue":"0"},{"status":"OK","metricKey":"critical_violations","comparator":"GT","errorThreshold":"5","actualValue":"0"},{"status":"OK","metricKey":"vulnerabilities","comparator":"GT","errorThreshold":"0","actualValue":"0"}],"periods":[{"index":1,"mode":"previous_version","date":"2018-10-15T08:59:56+0000","parameter":"3.3.1"}],"ignoredConditions":false}}
{"projectStatus":{"status":"ERROR","conditions":[{"status":"ERROR","metricKey":"coverage","comparator":"LT","errorThreshold":"40","actualValue":"7.0"},{"status":"ERROR","metricKey":"duplicated_lines_density","comparator":"GT","errorThreshold":"10","actualValue":"12.7"},{"status":"ERROR","metricKey":"blocker_violations","comparator":"GT","errorThreshold":"0","actualValue":"4"},{"status":"ERROR","metricKey":"bugs","comparator":"GT","errorThreshold":"5","actualValue":"33"},{"status":"ERROR","metricKey":"critical_violations","comparator":"GT","errorThreshold":"5","actualValue":"15"},{"status":"ERROR","metricKey":"vulnerabilities","comparator":"GT","errorThreshold":"0","actualValue":"13"}],"periods":[{"index":1,"mode":"previous_version","date":"2019-05-31T15:18:34+0000","parameter":"1.28.1"}],"ignoredConditions":false}}

实际结果(示例):

  • 错误....
  • 好的...。
  • 错误...
  • 错误...
  • 好的...

预期结果:

  • 错误....
  • 错误...
  • 错误...

0 个答案:

没有答案