Advertisement
gonzalob

Untitled

Jun 4th, 2021
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package app;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args)
  10.     {
  11.        
  12.             JSONtoJava(devolverJSON());
  13.  
  14.     }
  15.    
  16.     public static void JSONtoJava(String json)
  17.     {
  18.         try
  19.         {
  20.             //JSONArray array = new JSONArray(json);
  21.             JSONObject jsonObject = new JSONObject(json);
  22.             System.out.println(jsonObject.getString("apellido"));
  23.            
  24.             JSONObject jsonObject_tel;
  25.            
  26.             JSONArray telefonos = jsonObject.getJSONArray("telefonos");
  27.             for (int i = 0; i<telefonos.length();i++)
  28.             {
  29.                 jsonObject_tel = telefonos.getJSONObject(i);
  30.                 System.out.println(jsonObject_tel.getString("num"));
  31.                 System.out.println(jsonObject_tel.getBoolean("esFijo"));   
  32.             }
  33.            
  34.            
  35.             String direccion = jsonObject.getJSONObject("direccion").getString("calle");
  36.             System.out.println(direccion);
  37.            
  38.            
  39.            
  40.         }
  41.         catch (JSONException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.    
  46.     public static String devolverJSON()
  47.     {
  48.         String jsonToString = "";
  49.         try
  50.         {
  51.             JSONObject miPrimerObjetoJSON = new JSONObject();
  52.            
  53.             JSONObject unaDireccion = new JSONObject();
  54.            
  55.             JSONArray misTelefonos = new JSONArray();
  56.            
  57.             JSONObject unTelefono = new JSONObject();
  58.             unTelefono.put("num", "2235161198");
  59.             unTelefono.put("esFijo", false);
  60.            
  61.             JSONObject otroTelefono = new JSONObject();
  62.             otroTelefono.put("num", "2234708963");
  63.             otroTelefono.put("esFijo", true);
  64.            
  65.             misTelefonos.put(unTelefono);
  66.             misTelefonos.put(otroTelefono);
  67.            
  68.             unaDireccion.put("calle", "salta");
  69.             unaDireccion.put("numero", 4021);
  70.        
  71.             miPrimerObjetoJSON.put("edad",32);
  72.             miPrimerObjetoJSON.put("nombre", "gonzalo");
  73.             miPrimerObjetoJSON.put("apellido", "benoffi");
  74.             miPrimerObjetoJSON.put("trabaja", true);
  75.             miPrimerObjetoJSON.put("peso", 88.5);
  76.             miPrimerObjetoJSON.put("direccion", unaDireccion);
  77.             miPrimerObjetoJSON.put("telefonos",misTelefonos);
  78.            
  79.             jsonToString = miPrimerObjetoJSON.toString();
  80.            
  81.         }
  82.         catch (JSONException e) {
  83.             e.printStackTrace();
  84.         }
  85.        
  86.         return jsonToString;
  87.     }
  88.  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement