Get information from an ObjectJson without Array

0

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.

    
asked by anonymous 30.11.2017 / 15:47

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 override the server return from {"cliente":[{"id":"1334","nome":"Bruno"}]} to {"name":"Bruno"} , you can parse using only the following lines within the try-catch block:

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

For future implementations, I suggest you consider using libraries such as Retrofit to make these queries. This in particular saves you from manipulating JSON, and can focus on the parameters and returns of the HTTP call, both already serialized / deserialized.

EDIT:

I executed the code below in a jUnit test unit and it was executed successfully (changing the first parameter of assertEquals causes the test to end in 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 that you are passing as a parameter must be in a different format than the one you specified in the question, so the JsonException is being posted. You can use the debugger or Logcat to find out the real value of your JSONObject and then adapt the code accordingly.

    
01.12.2017 / 18:33