Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import org.json.simple.JSONArray;
  4. import org.json.simple.JSONObject;
  5.  
  6. public class JsonExample {
  7. public static void main(String[] args) {
  8.  
  9. JSONObject obj = new JSONObject();
  10.  
  11. for (Object algoritmo : algoritmos){
  12. JSONArray tempos = new JSONArray();
  13. tempos.add(algoritmo.tempo_ms);
  14. tempos.add(algoritmo.tempo_ns);
  15.  
  16. obj.put(algoritmo.nome, tempos);
  17. }
  18. try {
  19.  
  20. FileWriter file = new FileWriter("/caminho/do/arquivo");
  21. file.write(obj.toJSONString());
  22. file.flush();
  23. file.close();
  24.  
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30.  
  31. {
  32. "Insertion iterativo": ["7841910ns", "7ms"],
  33. "Selection iterativo": ["2677762ns", "2ms"],
  34. "Merge iterativo": ["708154ns", "0ms"]
  35. }
  36.  
  37. //Instancia objeto responsável por manipular JSON
  38. Gson gson = new Gson();
  39.  
  40. //Transformar objeto em JSON
  41. String json = gson.toJson(meuObjeto);
  42.  
  43. //Transformar JSON em Objeto
  44. meuObjeto = gson.fromJson(json, MeuObjeto.class);
  45.  
  46. String nome = "Joao";
  47. Integer idade = 30;
  48.  
  49. JsonObject json = Json.createObjectBuilder()
  50. .add("nome", nome)
  51. .add("idade", idade)
  52. .build();
  53.  
  54. String jsonString = json.toString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement