0
I have the class below and I can find the information of a Jsonarray in this format:
{"client":[{"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 doubt similar to mine https://stackoverflow.com/questions/33742944/how-to-parse-json-object-android-studio , but I could not apply to my example.
He had already tried this way but is informed:
– Bruno Mendonça
W/System.err: org.json.Jsonexception: No value for name
– Bruno Mendonça
@Brunomendonça updated the answer with an explanation.
– Mathias Berwig