How to access a field (value) of a JSON

Asked

Viewed 177 times

-1

I got a problem, I got this JSON {"cidade":{"nome":"Mag�","uf":"RJ","atualizacao":"2019-07-27","previsao":[{"dia":"2019-07-27","tempo":"pn","maxima":29,"minima":17,"iuv":6},{"dia":"2019-07-28","tempo":"vn","maxima":26,"minima":19,"iuv":6},{"dia":"2019-07-29","tempo":"vn","maxima":26,"minima":16,"iuv":6},{"dia":"2019-07-30","tempo":"pn","maxima":28,"minima":17,"iuv":6}]}} , I just need to take the first data from the "maxima" key and what I see on the internet are solutions using serialization, I do not want to save all this data. Another question, this JSON is correct in the matter of formatting ? How do I access it in Android Studio ?

  • to validate json I usually use this site https://jsonlint.com/. In your json you have an error from have an extra } the penultimate is left.

1 answer

0


In your case is an array within another Voce array has to go "navigating" and picking up the nodes;

I put inside an Asynctask and onPostExecute method do the reading treatment.

I do so to read values

JSONObject obj = null;
JSONArray jsonArray = null;

try {
  obj = new JSONObject(jsonResposta);

  jsonArray = (JSONArray) obj.get("ev_resposta");

} catch (JSONException e) {
  log("JSONException "+e.getMessage()+"\n"+this.getClass().getName().toString()+"\n onPostExecute");
}

You can also do obj.get(1); to read, placing inside a for and reading record to record.

Browser other questions tagged

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