Posted by : Unknown
Sabtu, 26 Mei 2018
Menampilkan json tanpa Array
final TextView t=(TextView)findViewById(R.id.txt);
Button b=(Button)findViewById(R.id.btn);
b.setOnClickListener(new Button.OnClickListener(){@Override
public void onClick(View p1)
{String jsonString = "{\"pelatih\":{\"nama\":\"adi saka\",\"kota\":\"Langit\",\"murid\":[{\"nama\":\"sila gading\",\"umur\":6},{\"nama\":\"putri tunggal\",\"umur\":8},{\"nama\":\"anka satu\",\"umur\":7}]}}";
try {
JSONObject object = new JSONObject(jsonString);
JSONObject objData = object.getJSONObject("pelatih");
for(int i = 0; i < objData.length(); i++){
t.setText(objData.getString("nama")+" "+objData.getString("kota"));}
} catch (JSONException e) {
e.printStackTrace();}
}
});