Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileWriter;
- import java.io.IOException;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- public class JsonExample {
- public static void main(String[] args) {
- JSONObject obj = new JSONObject();
- for (Object algoritmo : algoritmos){
- JSONArray tempos = new JSONArray();
- tempos.add(algoritmo.tempo_ms);
- tempos.add(algoritmo.tempo_ns);
- obj.put(algoritmo.nome, tempos);
- }
- try {
- FileWriter file = new FileWriter("/caminho/do/arquivo");
- file.write(obj.toJSONString());
- file.flush();
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- {
- "Insertion iterativo": ["7841910ns", "7ms"],
- "Selection iterativo": ["2677762ns", "2ms"],
- "Merge iterativo": ["708154ns", "0ms"]
- }
- //Instancia objeto responsável por manipular JSON
- Gson gson = new Gson();
- //Transformar objeto em JSON
- String json = gson.toJson(meuObjeto);
- //Transformar JSON em Objeto
- meuObjeto = gson.fromJson(json, MeuObjeto.class);
- String nome = "Joao";
- Integer idade = 30;
- JsonObject json = Json.createObjectBuilder()
- .add("nome", nome)
- .add("idade", idade)
- .build();
- String jsonString = json.toString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement