How to print information that is in json format but is printed in a way that gives the user to read?

2

The application is printing in this json format but I do not want it to print like this. For example I just want you to print "translation": "Hi dear" and only. I'm using eclipse and the java server language, how do you do?

I'm using an API but I need to show the data I get from this api when I put it to print with System.out.println (result);

You are printing this way:

 {
   "character_count": 8,
  "translations": [
{
  "translation": "Oi querida"
}
],
 "word_count": 2
}
    
asked by anonymous 29.06.2016 / 16:57

1 answer

2
The best way to do this would be to structure your JSON string, ie to include it in some data structure to be able to access / modify its values in a simpler way, so I would use a library to transform a JSON String into a object, such as a Map or its own object to represent that JSON, follows a library plus reference code for you to use that would solve this problem.

The name of the library is Gson, it is maintained by google, and is very popular in the Java world.

Sample code:

Gson gson = new Gson(); 
String json = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
Map<String,Object> map = new HashMap<String,Object>();
map = (Map<String,Object>) gson.fromJson(json, map.getClass());
System.out.println(map.get("k1")); //Saida = v1

References.

Repository on github: link

Documentation: link

    
29.06.2016 / 17:44