【问题标题】:JAVA :How to parse json Object to get the Keys and value?JAVA:如何解析 json 对象以获取键和值?
【发布时间】:2019-08-10 20:16:55
【问题描述】:

我能够从网站提取数据并得到响应

 {"vulnerability":{"id":15017916,"status":"open","closed_at":null,"created_at":"2019-07-26T10:06:03Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-flash-apsb14-21-cve-2014-0556"],"last_seen_time":"2019-07-24T06:00:00.000Z","fix_id":691,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-flash-apsb14-21-cve-2014-0556","open":true}],"asset_id":291633,"connectors":[{"id":7,"name":"Nexpose Enterprise","connector_definition_name":"Nexpose Enterprise","vendor":"Rapid7"}],"service_ticket":null,"urls":{"asset":"dummy.com/assets/291633"},"patch":true,"patch_published_at":"2014-09-11T07:57:40.000Z","cve_id":"CVE-2014-0556","cve_description":"Heap-based buffer overflow in Adobe Flash Player before 13.0.0.244 and 14.x and 15.x before 15.0.0.152 on Windows and OS X and before 11.2.202.406 on Linux, Adobe AIR before 15.0.0.249 on Windows and OS X and before 15.0.0.252 on Android, Adobe AIR SDK before 15.0.0.249, and Adobe AIR SDK & Compiler before 15.0.0.249 allows attackers to execute arbitrary code via unspecified vectors, a different vulnerability than CVE-2014-0559.","cve_published_at":"2014-09-10T01:55:00.000Z","description":null,"solution":null,"wasc_id":null,"severity":10,"threat":10,"popular_target":false,"active_internet_breach":true,"easily_exploitable":true,"malware_exploitable":true,"predicted_exploitable":false,"custom_fields":[],"first_found_on":"2019-07-24T06:27:26Z","top_priority":true,"risk_meter_score":100,"closed":false}}

我想解析这个 json 对象以获取所有键和值,最终将其输入到 tableRow 对象。

目前我看到的唯一解决方案是解析从我创建的 get 方法中获得的字符串并自己构建一个 TableRow 对象,但我想知道是否有人有更好的解决方案可以分享?

【问题讨论】:

标签: java json get httpresponse tablerow


【解决方案1】:

https://stleary.github.io/JSON-java/

每天一个库,远离 StackOverflow。

用法:

JSONObject jo = new JSONObject(response);

// get all keys
jo.keys();

// values
for (String key: jo.keys()) {
    jo.get(key);
}

【讨论】:

  • 那么我基本上就不必编写自己的解析器了吗?
  • 没有。只需使用图书馆。您可以使用 Maven 导入它:mvnrepository.com/artifact/org.json/json
  • 花点时间浏览一下库,它抛出的异常和其他方法。
【解决方案2】:

你可以使用 Gson 库

class Data {
  Vulnerability vulnerability
}

class Vulnerability {
  long id;
  String status;
... }

Data data = new Gson().fromJson(yourString, Data.class);
System.out.println(data.vulnerability.status);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2014-03-18
    相关资源
    最近更新 更多