JSON with variable fields

Asked

Viewed 110 times

0

I get from a PHP that accesses the Mysql database a JSON that doesn’t always have the same fields, now it comes like this:

[{"clu_nome":"Sport Club Corinthians Paulista","clu_estado":"SP","clu_ordem":5,"pon_pontos":"1858","pon_jogos":"1298","pon_vitori":"557","pon_empate":"383","pon_derrot":"358","pon_golpro":"1727","pon_golcon":"1364","pon_saldo":"363","pon_aprove":"54"},{"clu_nome":"Sport Club Internacional","clu_estado":"RS","clu_ordem":9,"pon_pontos":"1847","pon_jogos":"1290","pon_vitori":"559","pon_empate":"356","pon_derrot":"375","pon_golpro":"1743","pon_golcon":"1334","pon_saldo":"409","pon_aprove":"54"}]

therefore, it may have other formats:

[{"clu_nome":"S\u00e3o Paulo Futebol Clube","clu_estado":"SP","clu_ordem":3,"pon_classi":"224"},{"clu_nome":"Sport Club Corinthians Paulista","clu_estado":"SP","clu_ordem":5,"pon_classi":"202"},{"clu_nome":"Gr\u00eamio Foot-Ball Porto Alegrense","clu_estado":"RS","clu_ordem":10,"pon_classi":"185"},{"clu_nome":"Sport Club Internacional","clu_estado":"RS","clu_ordem":9,"pon_classi":"185"},{"clu_nome":"Clube Atl\u00e9tico Mineiro","clu_estado":"MG","clu_ordem":1,"pon_classi":"182"}]

How do I select each field to mount on a Listview?

No example I see around helps do this.

  • I don’t quite understand your doubt, but Voce can iterate on the item as if it were a normal php array if you convert JSON. $arr = json_decode($json_banco);

  • Sorry, I forgot to say that I get JSON in an application in Android Studio, this is where I need to manipulate the data. If anyone has a practical example I will thank you very much

  • Is that so? https://stackoverflow.com/questions/10308452/how-to-convert-the-following-json-string-to-java-object

  • This answer is also in PHP, not Android Studio. The examples I see know what kind of field comes, but has field that sometimes comes and sometimes does not come. In the examples, always term "clu_name", but "pon_points" may go and may not come, depending on the query

  • That answer is java

  • I am not an expert in Java, I use the basics in Android Studio, I do not understand, but I will evaluate more calmly

  • What I need to do is take the name of each column and its content to compose a Listview, but I can’t reference the columns by name because I don’t know if the possible names will even exist in JSON. Maybe this will help you help me. I’ve done all kinds of research and I didn’t get this

  • Assemble the columns in a foreach by grabbing the keys in the array. So, no matter how it comes, it will work. What do you think?

  • Solved: Jsonarray jsonResponse = new Jsonarray(jsonStr); for (int i = 0; i < jsonResponse.length(); i++) { Jsonobject jsonChildNode = jsonResponse.getJSONObject(i); for (int j = 0; j < jsonChildNode.length(); j+) { Labels.add(jsonChildNode.Names().getString(j); Labels.add(jsonChildNode.getString(jsonChildNode.Names().getString(j)); } }

Show 4 more comments
No answers

Browser other questions tagged

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