🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android / json basit

 

1-) Android -  json basit

 

1-) Android - apache ile

 

gradle->build.gradle(module:app)

dependencies {

..

   compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

}

2-) normal

 

@Override

protected void onCreate(Bundle savedInstanceState) {
    
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);
    TextView output = (TextView) findViewById(R.id.
textjson);

   String strJson=
"{ \"Employee\" :[{\"id\":\"101\",\"name\":\"Sonoo Jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"Vimal Jaiswal\",\"salary\":\"60000\"}] }";
   
    String data =
"";
    
try {
        
// Create the root JSONObject from the JSON string.
        
JSONObject  jsonRootObject = new JSONObject(strJson);

        
//Get the instance of JSONArray that contains JSONObjects
        
JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");

        
//Iterate the jsonArray and print the info of JSONObjects
        
for(int i=0; i < jsonArray.length(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            
int id = Integer.parseInt(jsonObject.optString("id").toString());
            String name = jsonObject.optString(
"name").toString();
            
float salary = Float.parseFloat(jsonObject.optString("salary").toString());

            data +=
"Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
        }
        output.setText(data);
    }
catch (JSONException e) {e.printStackTrace();}
}

 2021 Ocak 18 Pazartesi
 431