按对象值渗透JSON响应

时间:2018-06-11 05:43:43

标签: javascript json

我从服务器收到以下java响应,我的要求是显示servlet详细信息。

JSON

我已经编写了以下Javacript代码段,以便通过这些值渗透if(dataStoreLogFileSize > 10 && "dataStoreLogLevel": "production".) 但是它无法正常工作。任何人都可以帮我这个。

JSON

+ ********************************************** ********************************** +

{
"web-app": {
    "servlet": [{
            "servlet-name": "cofaxCDS",
            "servlet-class": "org.cofax.cds.CDSServlet",
            "init-param": {
                "configGlossary:installationAt": "Philadelphia, PA",
                "configGlossary:adminEmail": "ksm@pobox.com",
                "configGlossary:poweredBy": "Cofax",
                "configGlossary:poweredByIcon": "/images/cofax.gif",
                "configGlossary:staticPath": "/content/static",
                "templateProcessorClass": "org.cofax.WysiwygTemplate",
                "templateLoaderClass": "org.cofax.FilesTemplateLoader",
                "templatePath": "templates",
                "templateOverridePath": "",
                "defaultListTemplate": "listTemplate.htm",
                "defaultFileTemplate": "articleTemplate.htm",
                "useJSP": false,
                "jspListTemplate": "listTemplate.jsp",
                "jspFileTemplate": "articleTemplate.jsp",
                "searchEngineListTemplate": "forSearchEnginesList.htm",
                "searchEngineFileTemplate": "forSearchEngines.htm",
                "searchEngineRobotsDb": "WEB-INF/robots.db",
                "useDataStore": true,
                "dataStoreClass": "org.cofax.SqlDataStore",
                "redirectionClass": "org.cofax.SqlRedirection",
                "dataStoreName": "cofax",
                "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
                "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
                "dataStoreUser": "sa",
                "dataStorePassword": "dataStoreTestQuery",
                "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
                "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
                "dataStoreInitConns": 10,
                "dataStoreLogFileSize": 10,
                "dataStoreMaxConns": 100,
                "dataStoreConnUsageLimit": 100,
                "dataStoreLogLevel": "production",
                "maxUrlLength": 500
            }
        },
        {
            "servlet-name": "cofaxTools",
            "servlet-class": "org.cofax.cms.CofaxToolsServlet",
            "init-param": {
                "templatePath": "toolstemplates/",
                "log": 1,
                "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
                "logMaxSize": "",
                "dataLog": 1,
                "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
                "dataLogMaxSize": "",
                "removePageCache": "/content/admin/remove?cache=pages&id=",
                "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
                "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
                "lookInContext": 1,
                "adminGroupID": 4,
                "betaServer": true,
                "dataStoreInitConns": 10,
                "dataStoreLogFileSize": 5,
                "dataStoreMaxConns": 100,
                "dataStoreConnUsageLimit": 100,
                "dataStoreLogLevel": "test",
                "maxUrlLength": 500
            }
        }
    ],
    "servlet-mapping": {
        "cofaxCDS": "/",
        "cofaxEmail": "/cofaxutil/aemail/*",
        "cofaxAdmin": "/admin/*",
        "fileServlet": "/static/*",
        "cofaxTools": "/tools/*"
    },

    "taglib": {
        "taglib-uri": "cofax.tld",
        "taglib-location": "/WEB-INF/tlds/cofax.tld"
    }
  }
}

2 个答案:

答案 0 :(得分:2)

您正在过滤错误的数组,dataStoreLogLeveldataStoreLogFileSize的数组位于data['web-app'][0]['servlet']['init-param'],因此您需要在.filter上应用data['web-app'].servlet阵列。

对于fileSize,您需要使用parseFloat来提取大小,因为它是一个字符串。

您需要修复.filter语法。

并使用,console.log结果:

const data = {
    "web-app": {
        "servlet": [{
                "servlet-name": "cofaxCDS",
                "servlet-class": "org.cofax.cds.CDSServlet",
                "init-param": {
                    "configGlossary:installationAt": "Philadelphia, PA",
                    "configGlossary:adminEmail": "ksm@pobox.com",
                    "configGlossary:poweredBy": "Cofax",
                    "configGlossary:poweredByIcon": "/images/cofax.gif",
                    "configGlossary:staticPath": "/content/static",
                    "templateProcessorClass": "org.cofax.WysiwygTemplate",
                    "templateLoaderClass": "org.cofax.FilesTemplateLoader",
                    "templatePath": "templates",
                    "templateOverridePath": "",
                    "defaultListTemplate": "listTemplate.htm",
                    "defaultFileTemplate": "articleTemplate.htm",
                    "useJSP": false,
                    "jspListTemplate": "listTemplate.jsp",
                    "jspFileTemplate": "articleTemplate.jsp",
                    "searchEngineListTemplate": "forSearchEnginesList.htm",
                    "searchEngineFileTemplate": "forSearchEngines.htm",
                    "searchEngineRobotsDb": "WEB-INF/robots.db",
                    "useDataStore": true,
                    "dataStoreClass": "org.cofax.SqlDataStore",
                    "redirectionClass": "org.cofax.SqlRedirection",
                    "dataStoreName": "cofax",
                    "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
                    "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
                    "dataStoreUser": "sa",
                    "dataStorePassword": "dataStoreTestQuery",
                    "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
                    "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
                    "dataStoreInitConns": 10,
                    "dataStoreLogFileSize": "1GB",
                    "dataStoreMaxConns": 100,
                    "dataStoreConnUsageLimit": 100,
                    "dataStoreLogLevel": "production",
                    "maxUrlLength": 500
                }
            },
            {
                "servlet-name": "cofaxTools",
                "servlet-class": "org.cofax.cms.CofaxToolsServlet",
                "init-param": {
                    "templatePath": "toolstemplates/",
                    "log": 1,
                    "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
                    "logMaxSize": "",
                    "dataLog": 1,
                    "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
                    "dataLogMaxSize": "",
                    "removePageCache": "/content/admin/remove?cache=pages&id=",
                    "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
                    "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
                    "lookInContext": 1,
                    "adminGroupID": 4,
                    "betaServer": true,
                    "dataStoreInitConns": 10,
                    "dataStoreLogFileSize": "0.5GB",
                    "dataStoreMaxConns": 100,
                    "dataStoreConnUsageLimit": 100,
                    "dataStoreLogLevel": "test",
                    "maxUrlLength": 500
                }
            }
        ],
        "servlet-mapping": {
            "cofaxCDS": "/",
            "cofaxEmail": "/cofaxutil/aemail/*",
            "cofaxAdmin": "/admin/*",
            "fileServlet": "/static/*",
            "cofaxTools": "/tools/*"
        },

        "taglib": {
            "taglib-uri": "cofax.tld",
            "taglib-location": "/WEB-INF/tlds/cofax.tld"
        }
    }
}

let foo = data['web-app'].servlet.filter( 
  object => 
    object['init-param'].dataStoreLogLevel === 'production' 
    && parseFloat(object['init-param'].dataStoreLogFileSize) >= 1 // change this to 10 for fileSize >= 10GB
);

console.log('Servlet Details Test', foo);

答案 1 :(得分:2)

您可以使用此代码获取大于1gb的dataStoreLogFileSize和生成dataStoreLogLevel。



const jsoncode = {
  "web-app": {
servlet: [
  {
    "servlet-name": "cofaxCDS",
    "servlet-class": "org.cofax.cds.CDSServlet",
    "init-param": {
      "configGlossary:installationAt": "Philadelphia, PA",
      "configGlossary:adminEmail": "ksm@pobox.com",
      "configGlossary:poweredBy": "Cofax",
      "configGlossary:poweredByIcon": "/images/cofax.gif",
      "configGlossary:staticPath": "/content/static",
      templateProcessorClass: "org.cofax.WysiwygTemplate",
      templateLoaderClass: "org.cofax.FilesTemplateLoader",
      templatePath: "templates",
      templateOverridePath: "",
      defaultListTemplate: "listTemplate.htm",
      defaultFileTemplate: "articleTemplate.htm",
      useJSP: false,
      jspListTemplate: "listTemplate.jsp",
      jspFileTemplate: "articleTemplate.jsp",
      searchEngineListTemplate: "forSearchEnginesList.htm",
      searchEngineFileTemplate: "forSearchEngines.htm",
      searchEngineRobotsDb: "WEB-INF/robots.db",
      useDataStore: true,
      dataStoreClass: "org.cofax.SqlDataStore",
      redirectionClass: "org.cofax.SqlRedirection",
      dataStoreName: "cofax",
      dataStoreDriver: "com.microsoft.jdbc.sqlserver.SQLServerDriver",
      dataStoreUrl:
        "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
      dataStoreUser: "sa",
      dataStorePassword: "dataStoreTestQuery",
      dataStoreTestQuery: "SET NOCOUNT ON;select test='test';",
      dataStoreLogFile: "/usr/local/tomcat/logs/datastore.log",
      dataStoreInitConns: 10,
      dataStoreLogFileSize: "1GB",
      dataStoreMaxConns: 100,
      dataStoreConnUsageLimit: 100,
      dataStoreLogLevel: "production",
      maxUrlLength: 500
    }
  },
  {
    "servlet-name": "cofaxTools",
    "servlet-class": "org.cofax.cms.CofaxToolsServlet",
    "init-param": {
      templatePath: "toolstemplates/",
      log: 1,
      logLocation: "/usr/local/tomcat/logs/CofaxTools.log",
      logMaxSize: "",
      dataLog: 1,
      dataLogLocation: "/usr/local/tomcat/logs/dataLog.log",
      dataLogMaxSize: "",
      removePageCache: "/content/admin/remove?cache=pages&id=",
      removeTemplateCache: "/content/admin/remove?cache=templates&id=",
      fileTransferFolder:
        "/usr/local/tomcat/webapps/content/fileTransferFolder",
      lookInContext: 1,
      adminGroupID: 4,
      betaServer: true,
      dataStoreInitConns: 10,
      dataStoreLogFileSize: "0.5GB",
      dataStoreMaxConns: 100,
      dataStoreConnUsageLimit: 100,
      dataStoreLogLevel: "test",
      maxUrlLength: 500
    }
  }
],
"servlet-mapping": {
  cofaxCDS: "/",
  cofaxEmail: "/cofaxutil/aemail/*",
  cofaxAdmin: "/admin/*",
  fileServlet: "/static/*",
  cofaxTools: "/tools/*"
},

taglib: {
  "taglib-uri": "cofax.tld",
  "taglib-location": "/WEB-INF/tlds/cofax.tld"
}
  }
};

// jsoncode is your json response from server
const final = jsoncode['web-app'].servlet.filter((s) => {
  // get the number from dataStoreLogFileSize
  const size = s['init-param'].dataStoreLogFileSize.replace(/[^0-9\.]+/g, '');
  // make sure the size is a number by adding a plus sign
  return +size >= 1 && s['init-param'].dataStoreLogLevel === 'production';
});

console.log(final);