Java org.json.JSONObject create from String

Description

Java org.json.JSONObject create from String


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 w  w  . j a  v a2 s. c  om
   + "}";  
   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