简体   繁体   中英

How do I get specific parts of JSON data and save it as a String

I want to take some data from this JSON file and save it as a String variable. I am using Google Distance Matrix API and the result is given as a JSON. I wanted to save some parts of the output as a String variable.

public static void main(String[] args) throws IOException {
GoogMatrixRequest request = new GoogMatrixRequest();
String url_request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=n178je&destinations=n113ns&mode=driving&key=" + API_KEY;

String response = request.run(url_request);
System.out.println(response);

This is what is the output and what I want to retrieve is in here.

I want to take the "distance", "value" (7593) and save it as a String variable. Can someone tell me how I could do this?

First, your response is already a String. If you try to get part of it, it would be parsing a string. And since it's json string, there are many json mapper jar you can try, such as Gson, JacksonMapper, simpleJSON...

And for your particular case, I would suggest you to look into https://github.com/googlemaps/google-maps-services-java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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