简体   繁体   English

JAVA:如何解析json对象以获得键和值?

[英]JAVA :How to parse json Object to get the Keys and value?

I was able to pull data from a site and got a response 我能够从站点提取数据并得到响应

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

I want to parse this json object to get all the keys and values to ultimately input it to an tableRow object. 我想解析此json对象以获取所有键和值,以最终将其输入到tableRow对象中。

The only solution I see for the moment is parsing the strings I get from the get method I have created and build a TableRow object on my own but I was wondering if someone had a better solution to share? 我目前看到的唯一解决方案是解析从我创建的get方法获得的字符串,并自行构建TableRow对象,但我想知道是否有人可以共享更好的解决方案?

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

One library a day, keeps StackOverflow away. 每天只有一个库,这使StackOverflow远离了。

Usage: 用法:

JSONObject jo = new JSONObject(response);

// get all keys
jo.keys();

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

You can use Gson library 您可以使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM