Android parse JSON Key dynamically

 

JavaScript Object Notation
 

JSON stands for JavaScript Object Notation. It is an independent data exchange format and is the best alternative for XML. As a refresher, there are four main components of a JSON file: JSON array, JSON object, Key, and Value. Usually, we need to get the value of the specified key. But the key is sometimes vital, and you need to get it like some Currency Exchange APIs. In this article, we will know how to parse a key dynamically by tackling two examples.

First, Parse the JSON key with the value:

In the first example, we need to get the currency code which are the keys.

 

Parse the JSON key with the value

In the above image, the currency codes are inside the red box and the double values beside them are their rates. So, to create our model class for it, you will define a Map of String as a key and double as a value representing currency codes and their rates.

@SerializedName("rates") 
val rates: Map<String, Double>, 

package com.marwaeltayeb.currencyexchange.data.model
import com.google.gson.annotations.SerializedName
data class RateApiResponse(
@SerializedName("rates")
val rates: Map<String, Double>,
@SerializedName("base")
val base: String,
@SerializedName("date")
val date: String
)

Currency codes => keys (String)

Currency rates => values (double)

To access them in your adapter, you have to define pair variable

val currentRate: Pair<String, Double> = getItem(position)
val currencyCode = currentRate.first    // code
val currencyRate = currentRate.second   // rate

Each item of this map is a pair of String key and double value. So, you can get all items as a list or set and send them to the adapter and display them.

Second, Parse JSON nested keys with its value

If your value is another object, and you want the key and value from it,
you should create a map of string as a key and another map as a value. That means you need to get nested keys, and you should create a map of string as a key and another map as a value.

 

Parse JSON nested keys with its value

In the above image, your key is the date inside the red box and your value is the currency code and currency rate which will be represented by another Map.

So, to create our model class for it, we will define a Map of string as a key and another map as a value.

@SerializedName("rates") 
val rates: Map<String, Map<String, Double>>,

package com.marwaeltayeb.currencyexchange.data.model
import com.google.gson.annotations.SerializedName
data class HistoricApiResponse(
@SerializedName("base")
val base: String,
@SerializedName("end_date")
val endAt: String,
@SerializedName("rates")
val rates: Map<String, Map<String, Double>>,
@SerializedName("start_date")
val startAt: String
)

key (String)=> Dates

Value (ِAnother Map)

Currency codes => key (String)

Currency rates => values (double)

To access them in your activity, you need to observe the data and get it.

val response = data.rates

val dates = response.keys
val codes = response.values.keys 
val rates= response.values.values

Those are two examples of the concept that can help you to parse JSON in advanced and complex APIs. They are implemented in my app Currency Exchange. So enjoy using them in your applications.

Happy Coding!

Find me on GitHub | LinkedIn | Twitter

 
 
 

Comments

Popular posts from this blog

Hide and Keep your API key out of GitHub repository

Build an E-commerce Store App like Souq.com for Android