How to treat json in java

Asked

Viewed 60 times

1

I’m trying to treat the following json:

[{"code":"ARS","codein":"BRL","name":"Peso Argentino (R$)","high":"0.165","low":"0.1638","pctChange":"0.182","open":"0","bid":"0.1643","ask":"0.1647","varBid":"0.0003","timestamp":"1522699200000","create_date":"2018-04-02 20:20:08"}]

I’m using:

JSONObject json = new JSONObject(responseBody);
            JSONArray dados = json.getJSONArray("NÃO EXISTE UMA KEY?");
                for (int i = 0; i < dados.length(); ++i) {
                JSONObject linha = dados.getJSONObject(i);
                Log.v("valor", linha.getString("high"));
            }

How do I when there’s no key?

  • 1

    JSONArray dados = json.getJSONArray($seu_json); was not ?

  • 1

    @RBZ Just a little correction, in Java is not used the $ in the name of variables.

  • Oops ! That marked... is the addiction of PHP ! :D

  • Thanks @RBZ I hadn’t tried, it worked here.

1 answer

3


Thanks @RBZ I hadn’t tried, it worked here. - Emerson Barcellos

Answer:

JSONArray dados = json.getJSONArray(seu_json);

I learned a golden rule, basically like this:

[ = Jsonarray

{ = Jsonobject

Browser other questions tagged

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