Getting information from an Objectjson without Array

Asked

Viewed 169 times

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.

1 answer

0

Isolating the code responsible for Parsing, we have:

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();
}

If you overwrite the server return from {"cliente":[{"id":"1334","nome":"Bruno"}]} for {"name":"Bruno"}, can do Parsing using only the following lines inside the block try-catch:

    String nome = json.getString(TAG_NOME);
    nome_usuario.setText(nome);

For future implementations, I suggest you study the possibility of using libraries such as Retrofit to make those appointments. This specifically saves you from manipulating JSON, and can focus on the parameters and returns of the HTTP call, both already serialized/deserialized.

EDIT:

I ran the code below in a junit test unit and it was successfully executed (by changing the first parameter of assertEquals causes the test to end with error, as expected).

@Test
public void decodeJson() throws JSONException {
    final JSONObject json = new JSONObject("{\"name\":\"Bruno\"}");
    final String name = json.getString("name");
    assertEquals("Bruno", name);
}

The JSON you are passing as a parameter must be in a different format than you specified in the question, so the JsonException is being launched. You can use the scrubber or Logcat to discover the real value of your JSONObject and then adapt the code accordingly.

  • He had already tried this way but is informed:

  • W/System.err: org.json.Jsonexception: No value for name

  • @Brunomendonça updated the answer with an explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.