gonzalob

Untitled

Jun 3rd, 2020
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. package json;
  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 String CLAVE_NOMBRE ="nombre";
  10.  
  11.     public static void main(String[] args)
  12.     {
  13.        
  14.        
  15.         try
  16.         {
  17.             //jsonObject();
  18.             //jsonArray();
  19.            
  20.             //getJsonObject();
  21.             decodeJsonObject();
  22.         }
  23.         catch (JSONException e)
  24.         {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         }
  28.        
  29.     }
  30.    
  31.    
  32.     public static void decodeJsonObject() throws JSONException
  33.     {
  34.         JSONObject jsonObject = new JSONObject(getJsonObject());//{   }
  35.         //JSONArray jsonArray = new JSONArray(getJsonObject());// ESTA MAL // [ ]
  36.        
  37.         Persona persona = new Persona();
  38.        
  39.         if ((jsonObject.has("activo"))&&(!jsonObject.isNull("activo")))
  40.         {
  41.                 System.out.println(jsonObject.getBoolean("activo"));
  42.                 persona.setActivo(jsonObject.getBoolean("activo"));
  43.         }
  44.         else
  45.                 System.out.println("clave mal");
  46.        
  47.         JSONObject jsonObject2 = new JSONObject();
  48.         jsonObject2 = jsonObject.getJSONObject("direccion");
  49.         System.out.println(jsonObject2.getString("calle"));
  50.        
  51.         JSONArray jsonArray = new JSONArray();
  52.         jsonArray = jsonObject.getJSONArray("numeros");
  53.        
  54.         for (int i=0;i<jsonArray.length();i++)
  55.         {
  56.             JSONObject jsonObjectArray = jsonArray.getJSONObject(i);
  57.             System.out.println(jsonObjectArray.getString("tipo"));
  58.             System.out.println(jsonObjectArray.getString("numero"));
  59.         }
  60.                
  61.                
  62.     }
  63.    
  64.     public static JSONObject jsonObject() throws JSONException
  65.     {
  66.         JSONObject jsonObject = new JSONObject();
  67.        
  68.         jsonObject.put("activo", true);
  69.         jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
  70.         jsonObject.put("edad", 31);
  71.         jsonObject.put("altura", 1.88);
  72.         jsonObject.put("nombre", "john lennon");
  73.        
  74.         jsonObject.put("numeros", jsonArray2());
  75.        
  76.         JSONObject jsonObject2 = new JSONObject();
  77.        
  78.         jsonObject2.put("calle", "calle falsa");
  79.         jsonObject2.put("numero", 123);
  80.        
  81.         jsonObject.put("direccion", jsonObject2);
  82.        
  83.         //System.out.println(jsonObject.toString());
  84.        
  85.        
  86.        
  87.        
  88.         return jsonObject;
  89.        
  90.     }
  91.    
  92.     public static String getJsonObject() throws JSONException
  93.     {
  94.         JSONObject jsonObject = new JSONObject();
  95.        
  96.         jsonObject.put("activo", true);
  97.         jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
  98.         jsonObject.put("edad", 31);
  99.         jsonObject.put("altura", 1.88);
  100.         jsonObject.put("nombre", "john lennon");
  101.        
  102.         jsonObject.put("numeros", jsonArray2());
  103.        
  104.         JSONObject jsonObject2 = new JSONObject();
  105.        
  106.         jsonObject2.put("calle", "calle falsa");
  107.         jsonObject2.put("numero", 123);
  108.        
  109.         jsonObject.put("direccion", jsonObject2);
  110.        
  111.         System.out.println(jsonObject.toString());
  112.        
  113.         return jsonObject.toString();
  114.        
  115.     }
  116.    
  117.     public static void jsonArray() throws JSONException
  118.     {
  119.         JSONArray array = new JSONArray();
  120.        
  121.         array.put(1);
  122.         array.put(2);
  123.         array.put(3);
  124.        
  125.         //System.out.println(array.toString());
  126.        
  127.         JSONArray array2 = new JSONArray();
  128.         array2.put(jsonObject());
  129.         array2.put(jsonObject());
  130.         array2.put(jsonObject());
  131.        
  132.         //System.out.println(array2.toString());
  133.     }
  134.    
  135.     public static JSONArray jsonArray2() throws JSONException
  136.     {
  137.         JSONArray array = new JSONArray();
  138.        
  139.         JSONObject jsonObject = new JSONObject();
  140.        
  141.         jsonObject.put("tipo", "trabajo");
  142.         jsonObject.put("numero", "223123456");
  143.        
  144.         array.put(jsonObject);
  145.         array.put(jsonObject);
  146.         array.put(jsonObject);
  147.        
  148.         return array;
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment