I have the class below and I can fetch information from a JSONArray in this format:
{"customer": [{"id": "1334", "name": "Bruno"}]}
TextView nome_usuario;
private static final String TAG_CLIENTE = "cliente";
private static final String TAG_NOME = "nome";
JSONArray cliente = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View grafico = inflater.inflate(R.layout.fragment_fragment_perfil, container, false);
nome_usuario = (TextView ) grafico.findViewById(R.id.txtNome);
new JSONParse().execute();
return grafico;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Atualizando");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl("url do json");
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
cliente = json.getJSONArray(TAG_CLIENTE);
JSONObject c = cliente.getJSONObject(0);
String nome = c.getString(TAG_NOME);
nome_usuario.setText(nome);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
But now I would like to work with a json in the following format:
{"name": "Bruno"}
I found a question similar to my link , but I could not apply to my example.