Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package json;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class Main
- {
- public static String CLAVE_NOMBRE ="nombre";
- public static void main(String[] args)
- {
- /*
- Persona persona = new Persona();
- persona.setActivo(true);
- JSONArray array = new JSONArray();
- try {
- array.put(persona.toJSONObject());
- persona.setActivo(false);
- array.put(persona.toJSONObject());
- JsonUtiles.grabar(array);
- JSONObject jsonArray2 = new JSONObject(JsonUtiles.leer());
- System.out.println(jsonArray2.length());
- } catch (JSONException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- */
- /*
- try
- {
- //jsonObject();
- //jsonArray();
- //getJsonObject();
- //decodeJsonObject();
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- */
- Avion a1 = new Avion("Boeing", "777X", "comercial" , 384);
- Avion a2 = new Avion("Boeing", "F-18", " caza polivalente" , 1);
- File file = new File("file2.dat");
- boolean append = file.exists(); // if file exists then append, otherwise create new
- try
- {
- FileOutputStream fout = new FileOutputStream(file, append);
- AppendableObjectOutputStream oout = new AppendableObjectOutputStream(fout, append);
- oout.writeObject(a1);
- oout.writeObject(a2);
- mostrarArchivo("file2.dat");
- }
- catch (EOFException e)
- {
- //es un error pero no pasa nada
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void mostrarArchivo(String nombreDelArchivo) throws IOException, ClassNotFoundException
- {
- Object aux;
- FileInputStream bn = new FileInputStream(nombreDelArchivo);
- ObjectInputStream fobj = new ObjectInputStream(bn);
- aux = fobj.readObject();
- while(aux!=null)
- {
- System.out.println(aux.toString());
- aux = fobj.readObject();
- }
- fobj.close();
- }
- public static void decodeJsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject(getJsonObject());//{ }
- //JSONArray jsonArray = new JSONArray(getJsonObject());// ESTA MAL // [ ]
- Persona persona = new Persona();
- if ((jsonObject.has("activo"))&&(!jsonObject.isNull("activo")))
- {
- System.out.println(jsonObject.getBoolean("activo"));
- persona.setActivo(jsonObject.getBoolean("activo"));
- }
- else
- System.out.println("clave mal");
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2 = jsonObject.getJSONObject("direccion");
- System.out.println(jsonObject2.getString("calle"));
- JSONArray jsonArray = new JSONArray();
- jsonArray = jsonObject.getJSONArray("numeros");
- for (int i=0;i<jsonArray.length();i++)
- {
- JSONObject jsonObjectArray = jsonArray.getJSONObject(i);
- System.out.println(jsonObjectArray.getString("tipo"));
- System.out.println(jsonObjectArray.getString("numero"));
- }
- System.out.println(persona);
- }
- public static JSONObject jsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("activo", true);
- jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
- jsonObject.put("edad", 31);
- jsonObject.put("altura", 1.88);
- jsonObject.put("nombre", "john lennon");
- jsonObject.put("numeros", jsonArray2());
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put("calle", "calle falsa");
- jsonObject2.put("numero", 123);
- jsonObject.put("direccion", jsonObject2);
- //System.out.println(jsonObject.toString());
- return jsonObject;
- }
- public static String getJsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("activo", true);
- jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
- jsonObject.put("edad", 31);
- jsonObject.put("altura", 1.88);
- jsonObject.put("nombre", "john lennon");
- jsonObject.put("numeros", jsonArray2());
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put("calle", "calle falsa");
- jsonObject2.put("numero", 123);
- jsonObject.put("direccion", jsonObject2);
- System.out.println(jsonObject.toString());
- return jsonObject.toString();
- }
- public static void jsonArray() throws JSONException
- {
- JSONArray array = new JSONArray();
- array.put(1);
- array.put(2);
- array.put(3);
- //System.out.println(array.toString());
- JSONArray array2 = new JSONArray();
- array2.put(jsonObject());
- array2.put(jsonObject());
- array2.put(jsonObject());
- //System.out.println(array2.toString());
- }
- public static JSONArray jsonArray2() throws JSONException
- {
- JSONArray array = new JSONArray();
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("tipo", "trabajo");
- jsonObject.put("numero", "223123456");
- array.put(jsonObject);
- array.put(jsonObject);
- array.put(jsonObject);
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment