Java org.json.JSONObject get object by name

Description

Java org.json.JSONObject get object by name


import org.json.JSONException; 
import org.json.JSONObject; 
 
public class Main { 
   private final static String  jString = "{"  
   + "    \"geodata\":  ["  
   + "        {"  
   + "                \"id\": \"1\","  
   + "                \"name\": \"CSS\","                   
   + "                \"gender\"  : \"female\","  
   + "                \"latitude\"  : \"37\","  
   + "                \"longitude\"  : \"-12\""             
   + "                }"  
   + "        },"  
   + "        {"  
   + "                \"id\": \"2\","  
   + "                \"name\": \"HTML\","           
   + "                \"gender\"  : \"male\","  
   + "                \"latitude\"  : \"37\","  
   + "                \"longitude\"  : \"-121\""             
   + "                }"  
   + "        }"  
   + "    ]"  /*from w ww .j a va 2 s  .  co m*/
   + "}";  
   private static JSONObject jObject = null; 

   public static void main(String[] args) throws JSONException { 
       jObject = new JSONObject(jString); 
       JSONObject geoObject = jObject.getJSONObject("geodata"); 

       String geoId = geoObject.getString("id"); 
           System.out.println(geoId); 

       String name = geoObject.getString("name"); 
       System.out.println(name); 

       String gender=geoObject.getString("gender"); 
       System.out.println(gender); 


       String lat=geoObject.getString("latitude"); 
       System.out.println(lat); 

       String longit =geoObject.getString("longitude"); 
       System.out.println(longit);                    
   } 
} 



PreviousNext

Related