Get data from JSONArray - android

I'm trying to parse data from my json with the following kotlin code:
val text = JSONObject(URL(request).readText())
val results = text.getJSONArray("results")
val name = results.getJSONObject(5).getString("name") // org.json.JSONException: Index 5 out of range [0..1)
json
{
summary: {
queryType: "NEARBY",
queryTime: 13,
numResults: 2,
offset: 0,
totalResults: 2,
fuzzyLevel: 1,
geoBias: {
lat: -37.736343,
lon: 145.152114
}
},
results: [
{
type: "POI",
id: "AU/POI/p0/77255",
score: -0.38554,
dist: 385.5365152133808,
info: "search:ta:0323405846509-AU",
poi: {
name: "La Gourmet",
However I'm getting the following error on my 3rd line:
org.json.JSONException: Index 5 out of range [0..1)
I'm not sure why I'm getting this error. I resorted to searching for name via index because .getJSONObject("poi") doesn't take a String. This is also concerning because the data may change so I would prefer to query the JSON via String.
Any idea?

results is an array, and your code tries to get the 5th element of the array. You need to get the first element, and then you can get poi by name.
val text = JSONObject(URL(request).readText())
val results = text.getJSONArray("results")
val result0 = results.getJSONObject(0)
val poi = result0.getJSONObject("poi")

Related

Need help parsing this type of json in Android using retrofit for API response handling

Please show me a way to make a proper model for this JSON so that I get "Indian Premier League" as the key and the array next to it as value. We can have multiple leagues as well in the json.
{
"keySeriesNews": {
"Indian Premier League": [
{
"id": 203,
"slug": "tata-ipl-2022-why-delhi-capitals-bought-shardul-thakur-for-inr-1075-crore",
"competition_id": 3269,
"image_id": 1203,
"image_caption": "Shardul Thakur in action",
"start_date": "2022-03-05 17:25:38",
"created_at": "2022-03-05 12:08:19",
"updated_at": "2022-04-15 06:50:30",
"headline": "TATA IPL 2022: Why Delhi Capitals bought Shardul Thakur for INR 10.75 crore",
"sport_id": 15,
"image": {
"id": 1203,
"file_name": "shardulthakur_new.webp",
"created_at": "2022-04-15 06:47:41",
"image_path": "https://stagingkisma.6lgx.com/storage/images/shardulthakur_new_320x320.webp"
},
"competition": {
"id": 3269,
"slug": "indian-premier-league-2",
"competition_name": "Indian Premier League"
}
}
]
}
}
I have used this model to parse in retrofit but it is not fetching any data from the API. It is completely blank. No data in it.
data class HomeNewsParentModel(
#SerializedName("keySeriesNews" ) var keySeriesNews: JSONObject? = JSONObject()
)
However, when I use this model, it fetches data and I can access it. But problem is that it is hardcoded. I mean, these models will not capture data if the league name changes in any case. Here are the models which captured data.
data class HomeNewsParentModel(
#SerializedName("keySeriesNews" ) var keySeriesNews: KeySeriesNews? = KeySeriesNews()
)
data class KeySeriesNews (
#SerializedName("Indian Premier League" ) var league : ArrayList<League> = arrayListOf()
)
data class League (
#SerializedName("id" ) var id : Int? = null,
#SerializedName("slug" ) var slug : String? = null,
#SerializedName("competition_id" ) var competitionId : Int? = null,
#SerializedName("image_id" ) var imageId : Int? = null,
#SerializedName("image_caption" ) var imageCaption : String? = null,
#SerializedName("start_date" ) var startDate : String? = null,
#SerializedName("created_at" ) var createdAt : String? = null,
#SerializedName("updated_at" ) var updatedAt : String? = null,
#SerializedName("headline" ) var headline : String? = null,
#SerializedName("sport_id" ) var sportId : Int? = null,
#SerializedName("image" ) var image : Image? = Image(),
#SerializedName("competition" ) var competition : Competition? = Competition()
)
I have coded for a parser on the generic side to handle key-value type JSON like this but the JSON object was empty when I used the first approach of the data model. I need to make a generic parser to fetch league names as well as their data in key-value format since there can be multiple leagues that can come in this response as well.
PS: This is my parser which is getting empty JSON Object
private fun parseJSONData(data: JSONObject){
try {
val jsonObject = JSONObject(data)
for (key in jsonObject.keys()) {
Toast.makeText(
this#SeriesFragment.requireContext(),
"Key : " + key + " Value: " + jsonObject.optString(key),
Toast.LENGTH_SHORT
).show()
}
} catch (e: JSONException) {
e.printStackTrace()
}
}
Your help is much appreciated. Thanks.
Just a tip - if you already have the JSON available, you can use this plugin to easily generate a first draft of your model and adapt it if needed.
Some questions:
If you can have multiple leagues in your response, shouldn't keySeriesNews also be a list and not just a JSON object? For example like this:
{
"keySeriesNews": [
{
"id": 203,
"title": "Indian Premier League",
"slug": "tata-ipl-2022-why-delhi-capitals-bought-shardul-thakur-for-inr-1075-crore",
"competition_id": 3269,
"image_id": 1203,
...
}
]
}
What's your reasoning for handling JSON manually instead of using a ConverterFactory?
Where and how are you calling parseJsonData?
Well, I am not sure about this is correct or not. If anyone has a standard way of doing it, it is much appreciated. However, I have used the JSONElement instead of JSONObject or JSONArray and have used Map to handle key-value type data in my model, and GSONConvertorFactory has got this one right and fetched data correctly. This is the model I used:
data class HomeNewsParentModel(
#SerializedName("keySeriesNews" ) var keySeriesNews: HashMap<String, JsonElement>? = HashMap()
)
And I will parse JSONElement in my parseJsonData function to handle the key-value of this nonstandard JSON coming from API.
Hope this helped you in some way.

how to get value from list in dart/flutter?

I'm trying to get some currency data from a foreign exchange API. After getting data from the website,
I'm having a problem in extracting the desired value of currency pair from the list which I'm receiving after the HTTP request as data. Here's what I am doing:
Future<void> getInfo() async {
try {
print((url)); //url = EUR/USD, this value is comeing from a dropdown
URL =
"https://fcsapi.com/api-v2/forex/latest?symbol=$url&access_key=t58zo1uMFJZlNJxSrSmZv2qIUlSkCk9RAfCLkwnMwt1q1FFS";
print((URL));
Response response =
await http.get(Uri.encodeFull(URL),
headers: {"Accept": "application/json"});
data = jsonDecode(response.body);
print(data);
List info = data['response'];
print(info);
double price = ......; // I don't know what to write here I have tried everything.
print(price);
here I want to get the value of the price from the list "info" but idont know what to write in 'double price'
} catch (e) {
print('error is : $e');
}
}
this is the result on consol
I/flutter ( 4286): EUR/USD
I/flutter ( 4286): https://fcsapi.com/api-v2/forex/latest?
symbol=EUR/USD&access_key=t58zo1uMFJZlNJxSrSmZv2qIUlSkCk9RAfCLkwnMwt1q1FFS
I/flutter ( 4286): {status: true, code: 200, msg: Successfully,
response: [{
id: 1,
price: 1.1788,
change: +0.0072,
chg_per: +0.61%,
last_changed: 2020-10-05 14:47:57,
symbol: EUR/USD}],
info: {server_time: 2020-10-05 14:49:12 UTC,
credit_count: 1, _t: 2020-10-05 14:49:12 UTC}}
// this is printing due to print (data); statemet
I/flutter ( 4286): [{
id: 1,
price: 1.1788, // i want to print this value
change: +0.0072,
chg_per: +0.61%,
last_changed: 2020-10-05 14:47:57,
symbol: EUR/USD}]
// this is printing due to print (info); statemet, which is printing the responce of json
what i want :
i want to print the value of price in console after printing the responce e.g.
I/flutter ( 4286): 1.1788
this will work
double price = info[0]['price']
since the response is a list / array, you can use index to get items from the list.
var item = info[0]; // get first item from the list.
double price = item["price"];
print(price);

Return Array or object in Kotlin Dataclass

I'm pretty new to Kotlin so here I go.
I have some JSON data that sometimes returns an object or it can return an array. See example:
Object:
"planograms": {
"planogram": {
"multiple-facings": 1,
"planogram-seq": 838,
"planogram-id": 252871,
"planogram-desc": "Bulk Hardware - No Springs"
}
},
Array:
"planograms": {
"planogram": [
{
"multiple-facings": 1,
"planogram-seq": 112,
"planogram-id": 262027,
"planogram-desc": "Cat Food - Cat Dry"
},
{
"multiple-facings": 1,
"planogram-seq": 94,
"planogram-id": 276508,
"planogram-desc": "Cat Food - Cat Dry - A"
}
]
},
I am using Retrofit2 for the parsing so here is the code that does that:
fun getItemInfoExtended(#Body itemId: ItemData): Call<DefaultResponse>
DefaultResponse has more data that includes the object/array.
My question is how can I get to return the object or array. currently, I have a list in my data class, so when the object is returned I get an error stating the issue of not an array:
data class Planograms (
val planogram: List<Planogram>?
)
data class Planogram (
val multipleFacings: Long,
val planogramSeq: Long,
val planogramID: Long,
val planogramDesc: String
)
Thanks in advance for your input.

How to parse nested array insided a json object

So I made an api in laravel and it returns a response like this:
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email has already been taken."
],
"mobile": [
"The mobile has already been taken."
]
}
}
Can somebody show me how to get the specific values from errors?
You may create model representing your error json and use Gson to parse it. Here is some short example.
data class Errors(
val email: List<String>,
val phone: List<String>
)
data class YourErrorModel(
val message: String,
val errors: Errors
)
fun parseError(response: Response<*>): YourErrorModel? {
val errorBody = response.errorBody()?.string() ?: return null //No error body present
return Gson().fromJson(errorBody, YourErrorModel::class.java)
}
Also don't forget to handle nullable types in your response. And i suggest you to return just string, not array if that is exact error for field.
How about this :
JSONObject errorObject = yourJSONObject.optJSONObject("errors");
if (errorObject != null){
JSONArray emailMsgArray = errorObject.getJSONArray("email");
JSONArray mobileMsgArray = errorObject.getJSONArray("mobile");
String emailMsg= emailMsgArray.getString(0);
String mobileMsg= mobileMsgArray .getString(0);
}

Kotlin - Parse JSON

I have a json string with 2 keys error and user. First I want to check if error is not false and get the values from user.
Here is the Json String:
{
"error": false,
"user": {
"id": 26,
"name": "Someone",
"email": "someone#gmail.com",
"aktif": 1
}
}
How can I achieve this ?
Get the JsonObject "error" first :
val errorCheck = yourjsonresult.getJSONObject("error");
Then compare to check if it was false then:
if(errorCheck.equals("false")) { // or if it wasn't false -> !errorCheck.equals("false"))
val data = yourjsonresult.getJsonObject("user"); // get the user object
val name = data?.getString("name"); // or the other items
}
The result should be:
Someone
Also, arrays starts by [ but in your case, those are json objects which starts-ends by {}.

Categories

Resources