簡體   English   中英

Spring 引導 + Java:來自 JSON 數據的基於關鍵字的搜索

[英]Spring Boot + Java : Keyword Based Search from JSON data

我有一個使用 java 引導的 spring 項目。 我正在使用 Spring 啟動 2.1.4。 我使用REST實現了第三方設備並調用他們的 API。 我將響應存儲在彈性搜索中。 我正在使用彈性搜索 7.3。
I have one API which fetch data from elastic search and front end will call this API and render data which is JSON data from third party API. 現在我想構建一個可以返回搜索結果的 API。 例如有人在搜索框中輸入ip然后我必須從JSON數據中找到ip ,而不是來自彈性搜索並將結果返回到前端。
我知道如何從彈性中搜索數據,但我已經獲取了數據並進行了渲染。 搜索數據是另一個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: abuse@iana.org\nOrgAbuseRef: https://rdap.arin.net/registry/entity/IANA-IP-ARIN\nOrgTechHandle: IANA-IP-ARIN\nOrgTechName: ICANN\nOrgTechPhone: +1-310-301-5820 \nOrgTechEmail: abuse@iana.org\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數據,每次結構都會不同,每個 object 的鍵也會不同。
我的 Spring 啟動 API 必須有兩個 arguments 第一個是JSON是搜索的數據。 所以它應該看起來像

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

它必須從JSON數據中返回搜索到的文本,以便將結果提供給前端。
我不明白接下來要做什么。 請幫助我任何幫助和建議將不勝感激。

這可能對您有所幫助我對此進行了測試,因此我認為這對您有用。

        @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;
        }

如果這行得通,請告訴我。

以上代碼中的 Amith Walker 發生 NullpointerException 所以,忽略值為 Null 的鍵,例如, “值”:null

我也在上面開發代碼

// ListMap For Store the Searchable Data.
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();

 public List<Map<String, Object>> getKeyAndValues(String jsonString, String s) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject object;
        try {
            object = (JSONObject) new JSONParser().parse(jsonString);
            JSONObject obj2 = new JSONObject();
            JSONObject obj3 = new JSONObject();
            Iterator<?> setOfObject = object.keySet().iterator();
            while (setOfObject.hasNext()) {
                String key = (String) setOfObject.next();
                if (object.get(key) instanceof JSONObject) {
                    obj2 = (JSONObject) object.get(key);
                    getKeyAndValues(obj2.toJSONString(), s);
                }
                if (object.get(key) instanceof JSONArray) {
                    JSONArray array = (JSONArray) object.get(key);
                    Iterator<?> setOfArray = array.iterator();
                    while (setOfArray.hasNext()) {
                        obj3 = (JSONObject) setOfArray.next();
                        getKeyAndValues(obj3.toJSONString(), s);
                    }
                } else {
                    if (object.get(key) != null) {
                        if (key.contains(s)) {
                            map.put(key, object.get(key));
                        }
                        if (object.get(key).toString().contains(s)) {
                            map.put(key, object.get(key));
                        }
                    }
                }
            }
             // Only Non-Empty Map add into ListMap..
            if (!map.isEmpty()) {
                listMap.add(map);
            }
        } catch (ParseException e) {
            
        }
        return listMap;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM