gonzalob

Untitled

Jun 10th, 2020
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. package json;
  2.  
  3. import java.io.EOFException;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10.  
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15.  
  16.  
  17. public class Main
  18. {
  19.     public static String CLAVE_NOMBRE ="nombre";
  20.  
  21.     public static void main(String[] args)
  22.     {
  23.         /*
  24.         Persona persona = new Persona();
  25.         persona.setActivo(true);
  26.        
  27.         JSONArray array = new JSONArray();
  28.         try {
  29.             array.put(persona.toJSONObject());
  30.             persona.setActivo(false);
  31.             array.put(persona.toJSONObject());
  32.             JsonUtiles.grabar(array);
  33.            
  34.             JSONObject jsonArray2 = new JSONObject(JsonUtiles.leer());
  35.             System.out.println(jsonArray2.length());
  36.            
  37.         } catch (JSONException e1) {
  38.             // TODO Auto-generated catch block
  39.             e1.printStackTrace();
  40.         }
  41.         */
  42.        
  43.         /*
  44.         try
  45.         {
  46.             //jsonObject();
  47.             //jsonArray();
  48.            
  49.             //getJsonObject();
  50.             //decodeJsonObject();
  51.         }
  52.         catch (JSONException e)
  53.         {
  54.             // TODO Auto-generated catch block
  55.             e.printStackTrace();
  56.         }
  57.         */
  58.        
  59.         Avion a1 = new Avion("Boeing", "777X", "comercial" , 384);
  60.         Avion a2 = new Avion("Boeing", "F-18", " caza polivalente" , 1);      
  61.        
  62.        
  63.         File file = new File("file2.dat");
  64.         boolean append = file.exists(); // if file exists then append, otherwise create new
  65.  
  66.         try
  67.         {
  68.             FileOutputStream fout = new FileOutputStream(file, append);
  69.             AppendableObjectOutputStream oout = new AppendableObjectOutputStream(fout, append);
  70.             oout.writeObject(a1);
  71.             oout.writeObject(a2);
  72.             mostrarArchivo("file2.dat");
  73.         }
  74.         catch (EOFException e)
  75.         {
  76.             //es un error pero no pasa nada
  77.         }
  78.         catch (FileNotFoundException e)
  79.         {
  80.             e.printStackTrace();
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         } catch (ClassNotFoundException e) {
  84.             // TODO Auto-generated catch block
  85.             e.printStackTrace();
  86.         }
  87.        
  88.     }
  89.    
  90.      public static void mostrarArchivo(String nombreDelArchivo) throws IOException, ClassNotFoundException
  91.         {
  92.             Object aux;
  93.             FileInputStream bn = new FileInputStream(nombreDelArchivo);
  94.             ObjectInputStream fobj = new ObjectInputStream(bn);
  95.             aux = fobj.readObject();
  96.      
  97.             while(aux!=null)
  98.             {
  99.                 System.out.println(aux.toString());
  100.                 aux = fobj.readObject();  
  101.             }
  102.             fobj.close();
  103.     }
  104.    
  105.    
  106.     public static void decodeJsonObject() throws JSONException
  107.     {
  108.         JSONObject jsonObject = new JSONObject(getJsonObject());//{   }
  109.         //JSONArray jsonArray = new JSONArray(getJsonObject());// ESTA MAL // [ ]
  110.        
  111.         Persona persona = new Persona();
  112.        
  113.         if ((jsonObject.has("activo"))&&(!jsonObject.isNull("activo")))
  114.         {
  115.                 System.out.println(jsonObject.getBoolean("activo"));
  116.                 persona.setActivo(jsonObject.getBoolean("activo"));
  117.         }
  118.         else
  119.                 System.out.println("clave mal");
  120.        
  121.         JSONObject jsonObject2 = new JSONObject();
  122.         jsonObject2 = jsonObject.getJSONObject("direccion");
  123.         System.out.println(jsonObject2.getString("calle"));
  124.        
  125.         JSONArray jsonArray = new JSONArray();
  126.         jsonArray = jsonObject.getJSONArray("numeros");
  127.        
  128.         for (int i=0;i<jsonArray.length();i++)
  129.         {
  130.             JSONObject jsonObjectArray = jsonArray.getJSONObject(i);
  131.             System.out.println(jsonObjectArray.getString("tipo"));
  132.             System.out.println(jsonObjectArray.getString("numero"));
  133.         }
  134.                
  135.         System.out.println(persona);       
  136.     }
  137.    
  138.     public static JSONObject jsonObject() throws JSONException
  139.     {
  140.         JSONObject jsonObject = new JSONObject();
  141.        
  142.         jsonObject.put("activo", true);
  143.         jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
  144.         jsonObject.put("edad", 31);
  145.         jsonObject.put("altura", 1.88);
  146.         jsonObject.put("nombre", "john lennon");
  147.        
  148.         jsonObject.put("numeros", jsonArray2());
  149.        
  150.         JSONObject jsonObject2 = new JSONObject();
  151.        
  152.         jsonObject2.put("calle", "calle falsa");
  153.         jsonObject2.put("numero", 123);
  154.        
  155.         jsonObject.put("direccion", jsonObject2);
  156.        
  157.         //System.out.println(jsonObject.toString());
  158.        
  159.        
  160.        
  161.        
  162.         return jsonObject;
  163.        
  164.     }
  165.    
  166.     public static String getJsonObject() throws JSONException
  167.     {
  168.         JSONObject jsonObject = new JSONObject();
  169.        
  170.         jsonObject.put("activo", true);
  171.         jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
  172.         jsonObject.put("edad", 31);
  173.         jsonObject.put("altura", 1.88);
  174.         jsonObject.put("nombre", "john lennon");
  175.        
  176.         jsonObject.put("numeros", jsonArray2());
  177.        
  178.         JSONObject jsonObject2 = new JSONObject();
  179.        
  180.         jsonObject2.put("calle", "calle falsa");
  181.         jsonObject2.put("numero", 123);
  182.        
  183.         jsonObject.put("direccion", jsonObject2);
  184.        
  185.         System.out.println(jsonObject.toString());
  186.        
  187.         return jsonObject.toString();
  188.        
  189.     }
  190.    
  191.     public static void jsonArray() throws JSONException
  192.     {
  193.         JSONArray array = new JSONArray();
  194.        
  195.         array.put(1);
  196.         array.put(2);
  197.         array.put(3);
  198.        
  199.         //System.out.println(array.toString());
  200.        
  201.         JSONArray array2 = new JSONArray();
  202.         array2.put(jsonObject());
  203.         array2.put(jsonObject());
  204.         array2.put(jsonObject());
  205.        
  206.         //System.out.println(array2.toString());
  207.     }
  208.    
  209.     public static JSONArray jsonArray2() throws JSONException
  210.     {
  211.         JSONArray array = new JSONArray();
  212.        
  213.         JSONObject jsonObject = new JSONObject();
  214.        
  215.         jsonObject.put("tipo", "trabajo");
  216.         jsonObject.put("numero", "223123456");
  217.        
  218.         array.put(jsonObject);
  219.         array.put(jsonObject);
  220.         array.put(jsonObject);
  221.        
  222.         return array;
  223.     }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment