gpt4 book ai didi

java - Spring Boot + Java : Keyword Based Search from JSON data

转载 作者:行者123 更新时间:2023-12-01 12:47:49 24 4
gpt4 key购买 nike

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

    {
"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"
}
}

这是 sample 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 的搜索文本用于向前端提供结果的数据。
我不明白接下来要做什么。请帮助我任何帮助和建议将不胜感激。

最佳答案

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

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

If this will work then let me know.

关于java - Spring Boot + Java : Keyword Based Search from JSON data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com