Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Domas Vysniauskas
Domas Vysniauskas
9,546 Points

How to display jsonObject parsed from jsonArray to TextView?

Hi, I have a data model Currency with 3 fields and getter/setters. How can I display a single jsonObject for example a "name" that I get from jsonArray to a textView in displayData() method?

private void displayData() {

    how to set "name" jsonObject to nName textview?

}


private Currency[] parsingJson(String data) throws JSONException {
    JSONArray jsonArray = new JSONArray(data);

    Currency[] currencies = new Currency[data.length()];

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonCurrency = jsonArray.getJSONObject(i);
        Currency currency = new Currency();

        currency.setmName(jsonCurrency.getString("name"));
        currency.setmSymbol(jsonCurrency.getString("symbol"));
        currency.setmPriceUsd(jsonCurrency.getDouble("price_usd"));

        Log.d("TAG", currency.getmName() +" " + currency.getmSymbol() + " " + currency.getmPriceUsd());

        currencies[i] = currency;

    }

    return currencies;
} 

jsonArray data that I parsing from:

{ "id": "ethereum", "name": "Ethereum", "symbol": "ETH", "rank": "2", "price_usd": "201.802", "price_btc": "0.074059", "24h_volume_usd": "400531000.0", "market_cap_usd": "18890696672.0", "available_supply": "93610057.0", "total_supply": "93610057.0", "percent_change_1h": "-0.39", "percent_change_24h": "-0.81", "percent_change_7d": "-11.83", "last_updated": "1501230264" } ]