Spring Boot + Java:从JSON数据中基于关键字的搜索

问题描述 投票:4回答:1

我在春季启动中有一个使用Java的项目。我正在使用Spring Boot 2.1.4。我实现了第三方设备,并使用REST调用了它们的API。我将响应存储在弹性搜索中。我正在使用弹性搜索7.3。我有一个从弹性搜索中获取数据的API,前端将调用此API并呈现基本上是来自第三方API的JSON数据的数据。现在,我想构建一个可以返回搜索结果的API。例如,有人在搜索框中键入ip,那么我必须从ip数据中找到JSON,而不是从弹性搜索中找到,然后将结果返回到前端。我知道如何从弹性搜索数据,但是我已经获取了数据并进行了渲染。这是另一个用于搜索数据的API,我想从渲染的数据中搜索数据。因此,这基本上是使用java从JSON数据进行关键字搜索的方法。我做了很多研究,但找不到任何相关的内容。

    {
      "data": {
        "attributes": {
          "last_analysis_results": {
            "AlienVault": {
              "category": "harmless",
              "engine_name": "AlienVault",
              "method": "blacklist",
              "result": "clean"
            },
            "BADWARE.INFO": {
              "category": "harmless",
              "engine_name": "BADWARE.INFO",
              "method": "blacklist",
              "result": "clean"
            },
            "CINS Army": {
              "category": "harmless",
              "engine_name": "CINS Army",
              "method": "blacklist",
              "result": "clean"
            },
            "CLEAN MX": {
              "category": "harmless",
              "engine_name": "CLEAN MX",
              "method": "blacklist",
              "result": "clean"
            },
            "Cisco Talos IP Blacklist": {
              "category": "harmless",
              "engine_name": "Cisco Talos IP Blacklist",
              "method": "blacklist",
              "result": "clean"
            },
            "DNS8": {
              "category": "harmless",
              "engine_name": "DNS8",
              "method": "blacklist",
              "result": "clean"
            },
            "ESTsecurity-Threat Inside": {
              "category": "harmless",
              "engine_name": "ESTsecurity-Threat Inside",
              "method": "blacklist",
              "result": "clean"
            },
            "Fortinet": {
              "category": "harmless",
              "engine_name": "Fortinet",
              "method": "blacklist",
              "result": "clean"
            },
            "GreenSnow": {
              "category": "harmless",
              "engine_name": "GreenSnow",
              "method": "blacklist",
              "result": "clean"
            },
            "IPsum": {
              "category": "harmless",
              "engine_name": "IPsum",
              "method": "blacklist",
              "result": "clean"
            },
            "Kaspersky": {
              "category": "harmless",
              "engine_name": "Kaspersky",
              "method": "blacklist",
              "result": "clean"
            },
            "Nucleon": {
              "category": "harmless",
              "engine_name": "Nucleon",
              "method": "blacklist",
              "result": "clean"
            },
            "OpenPhish": {
              "category": "harmless",
              "engine_name": "OpenPhish",
              "method": "blacklist",
              "result": "clean"
            },
            "Phishtank": {
              "category": "harmless",
              "engine_name": "Phishtank",
              "method": "blacklist",
              "result": "clean"
            },
            "Quick Heal": {
              "category": "harmless",
              "engine_name": "Quick Heal",
              "method": "blacklist",
              "result": "clean"
            },
            "Sophos": {
              "category": "harmless",
              "engine_name": "Sophos",
              "method": "blacklist",
              "result": "clean"
            },
            "Tencent": {
              "category": "harmless",
              "engine_name": "Tencent",
              "method": "blacklist",
              "result": "clean"
            },
            "URLhaus": {
              "category": "harmless",
              "engine_name": "URLhaus",
              "method": "blacklist",
              "result": "clean"
            }
          },
          "last_analysis_stats": {
            "harmless": 75,
            "malicious": 0,
            "suspicious": 0,
            "timeout": 0,
            "undetected": 0
          },
          "last_modification_date": 1587640540,
          "reputation": 0,
          "tags": [
            "private"
          ],
          "total_votes": {
            "harmless": 0,
            "malicious": 0
          },
          "whois": "NetRange: 10.0.0.0 - 10.255.255.255\nCIDR: 10.0.0.0/8\nNetName: PRIVATE-ADDRESS-ABLK-RFC1918-IANA-RESERVED\nNetHandle: NET-10-0-0-0-1\nParent: ()\nNetType: IANA Special Use\nOriginAS: \nOrganization: Internet Assigned Numbers Authority (IANA)\nRegDate: \nUpdated: 2013-08-30\nComment: These addresses are in use by many millions of independently operated networks, which might be as small as a single computer connected to a home gateway, and are automatically configured in hundreds of millions of devices. They are only intended for use within a private context and traffic that needs to cross the Internet will need to use a different, unique address.\nComment: \nComment: These addresses can be used by anyone without any need to coordinate with IANA or an Internet registry. The traffic from these addresses does not come from ICANN or IANA. We are not the source of activity you may see on logs or in e-mail records. Please refer to http://www.iana.org/abuse/answers\nComment: \nComment: These addresses were assigned by the IETF, the organization that develops Internet protocols, in the Best Current Practice document, RFC 1918 which can be found at:\nComment: http://datatracker.ietf.org/doc/rfc1918\nRef: https://rdap.arin.net/registry/ip/10.0.0.0\nOrgName: Internet Assigned Numbers Authority\nOrgId: IANA\nAddress: 12025 Waterfront Drive\nAddress: Suite 300\nCity: Los Angeles\nStateProv: CA\nPostalCode: 90292\nCountry: US\nRegDate: \nUpdated: 2012-08-31\nRef: https://rdap.arin.net/registry/entity/IANA\nOrgAbuseHandle: IANA-IP-ARIN\nOrgAbuseName: ICANN\nOrgAbusePhone: +1-310-301-5820 \nOrgAbuseEmail: [email protected]\nOrgAbuseRef: https://rdap.arin.net/registry/entity/IANA-IP-ARIN\nOrgTechHandle: IANA-IP-ARIN\nOrgTechName: ICANN\nOrgTechPhone: +1-310-301-5820 \nOrgTechEmail: [email protected]\nOrgTechRef: https://rdap.arin.net/registry/entity/IANA-IP-ARIN\n",
          "whois_date": 1585897644
        },
        "id": "10.2.2.2",
        "links": {
          "self": "https://www.virustotal.com/api/v3/ip_addresses/10.2.2.2"
        },
        "type": "ip_address"
      }
    }

这是示例JSON数据,每个时间结构都不同,每个对象的键也不同。我的Spring Boot API必须有两个参数,第一个是JSON数据,第二个是搜索的文本。所以应该看起来像

    @PostMapping(value = "/search")
    public Object keywordSearch(@RequestBody JSONObject object, @RequestParam String s) throws Exception 
    {
                String string = object.toJSONString();
                // next operation
    }

它必须从JSON数据中返回搜索到的文本,以便将结果提供给前端。我不知道下一步该怎么做。请帮助我,任何帮助和建议将不胜感激。

java json spring-boot
1个回答
3
投票

这可能对您有帮助,我对此进行了测试,因此我认为这对您有用。

        @PostMapping(value = "/search")
        public Object keywordSearch(@RequestBody JSONObject object, @RequestParam String s) throws Exception {
            String js = object.toJSONString();
            JSONArray resultArray = new JSONArray();
            JSONObject jObj1 = (JSONObject) new JSONParser().parse(js);
            JSONObject jObj2 = new JSONObject();
            JSONObject jObj3 = new JSONObject();
            Iterator<?> setOfObject = jObj1.keySet().iterator();
            while (setOfObject.hasNext()) {
                String key = (String) setOfObject.next();
                if (jObj1.get(key) instanceof JSONObject) {
                    jObj2 = (JSONObject) jObj1.get(key);
                    getKeyAndValues(jObj2.toJSONString(), s);
                }
                if (jObj1.get(key) instanceof JSONArray) {
                    JSONArray jArr = (JSONArray) jObj1.get(key);
                    Iterator<?> setOfArray = jArr.iterator();
                    while (setOfArray.hasNext()) {
                        jObj3 = (JSONObject) setOfArray.next();
                        getKeyAndValues(jObj3.toJSONString(), s);
                    }
                } else {
                    if (key.contains(s)) {
                        resultArray.add(key);
                    }
                    if (jObj1.get(key).equals(s)) {
                        resultArray.add(jObj1.get(key));
                    }
                }
            }
            return resultArray;
        }

如果可以,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.