I need a list of objects and I'm having problems to get it. I'm pretty new using this so someone who can help me?
I'm using volley to get a jsonObject and then I need convert it(I saw the best wa to do this is with gson). Below you can see how looks an example of my json.
{
network: {
company: "JCDecaux",
href: "/v2/networks/dublinbikes",
id: "dublinbikes",
location: {
city: "Dublin",
country: "IE",
latitude: 53.3498053,
longitude: -6.2603097
},
name: "dublinbikes",
stations: [
{
empty_slots: 37,
extra: {
address: "Fitzwilliam Square East",
bonus: false,
connected: "1",
last_update: "1434047944",
open: true,
slots: 40,
ticket: true,
uid: 89
},
free_bikes: 3,
id: "153ff4dfb7bd8912ef91c10849129c2e",
latitude: 53.335211,
longitude: -6.2509,
name: "Fitzwilliam Square East",
timestamp: "2015-06-11T18:41:31.11Z"
},
{
empty_slots: 0,
extra: {
address: "Portobello Harbour",
bonus: false,
connected: "1",
last_update: "1434047764",
open: true,
slots: 30,
ticket: false,
uid: 34
},
free_bikes: 30,
id: "3c0cfd547a142bb651280991a412bcbe",
latitude: 53.330362,
longitude: -6.265163,
name: "Portobello Harbour",
timestamp: "2015-06-11T18:41:31.15Z"
},
... ect
Station class
public class Station {
public Station(){}
public String StationName;
public String Distance;
public String Slots;
public String Bikes;
public String LastUpdate;
//Getters and Setters ...
}
Below you can see what I have done so far..
//stations
JSONObject jsonNetwork = new JSONObject(response.getString("network"));
Type listType = new TypeToken<ArrayList<Station>>() {
}.getType();
List<Station> yourClassList = new Gson().fromJson(jsonNetwork, listType);
But I don't know how to parse all this and avoid the data I dont need and also the function Gson().fromJson needs and JsonArray and what I have is a JsonObject
Thanks for your time!
If all that you are looking for are the stations then I think you should do the following:
//stations
JSONObject jsonNetwork = new JSONObject(response.getString("network"));
JSONArray stationsArray = jsonNetwork.getJSONArray("stations");
Now, you should pass this stationsArray variable to the fromJson method. Also, the variable names of your Station class should be equal to the key names that you want to extract from your json. There is a good example in the following link:
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Use below code
Gson gson = new Gson();
YourClass obj = gson.fromJson(jsonObject.toString(), YourClass.class);
Related
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.
I have tried to read this similar thread in java in here
so I try the accepted answer there but it doesn't work. here is my problem
I will get two different JSON Response from an endpoint. if I successfully get the Restaurant data, the JSON will be like this
{
"R": {
"has_menu_status": {
"delivery": -1,
"takeaway": -1
},
"res_id": 18941862,
"is_grocery_store": false
},
"id": "18941862",
"name": "Pizza Maru",
"url": "https://www.zomato.com/jakarta/pizza-maru-1-thamrin?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "Grand Indonesia Mall, East Mall, Lantai 3A, Jl. M.H. Thamrin No. 1, Thamrin, Jakarta",
"locality": "Grand Indonesia Mall, Thamrin",
"city": "Jakarta",
"city_id": 74,
"latitude": "-6.1955810000",
"longitude": "106.8213770000",
"zipcode": "",
"country_id": 94,
"locality_verbose": "Grand Indonesia Mall, Thamrin, Jakarta"
},
"switch_to_order_menu": 0,
"cuisines": "Pizza",
"timings": "10 AM to 10 PM",
"average_cost_for_two": 180000,
"price_range": 3,
"currency": "IDR",
"thumb": "https://b.zmtcdn.com/data/pictures/chains/2/18941862/403aa36cb046e86a694e7989bb7cd545.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A",
"has_online_delivery": 0,
"is_delivering_now": 0,
"store_type": "",
"phone_numbers": "021 3108656",
}
then If I send invalid restaurantID then I will get error JSON Response like this:
{
"code": 404,
"status": "Not Found",
"message": "Not Found"
}
here is the data class I made
data class Restaurant (
#SerializedName("id")
val id : Int = 0,
#SerializedName("name")
var name : String = "",
#SerializedName("url")
val url : String = "",
#SerializedName("location")
val location : Location = Location(),
#SerializedName("currency")
val currency : String = "",
#SerializedName("phone_numbers")
val phone_numbers : String = "",
#SerializedName("thumb")
val thumbnail : String = ""
)
for successful Response
data class Location (
#SerializedName("address")
val address : String = "",
#SerializedName("city")
val city : String = "",
#SerializedName("latitude")
val latitude : Double = 0.0,
#SerializedName("longitude")
val longitude : Double = 0.0,
#SerializedName("zipcode")
val zipcode : String = ""
)
for Error Response
data class ErrorResponse (
val code : Int,
val status : String,
val message : String
)
here is my Interface for my Retrofit. the idea is, I will cast it as Any first, then I will downcast either to Restaurant or ZomatoErrorResponse
interface RestaurantAPI {
#Headers("user-key: $USER_KEY_ZOMATO")
#GET("restaurant")
fun getRestaurantDetail(
#Query("res_id") id: Int
): Call<Any>
}
here is the error:
so I use my retrofit like this
val call = restaurantService.getRestaurantDetail(restaurantID)
call.enqueue(object: Callback<Any>{
override fun onResponse(call: Call<Any>, response: Response<Any>) {
if (response.isSuccessful) {
// this line is executed
Log.d("checkxxx","${response.body()}")
val restaurantData = response.body() as Restaurant // <-- but Error while casting Any to Restaurant in here
restaurant.postValue(restaurantData)
}
}
})
my app crash at that line. but actually I can successfully get the data, but I fail to cast it to Restaurant.
here the logcat of my response.body()
what went wrong in here ?
or maybe there is a better approach than this one
I finally can solve my problem using this code below
val call = restaurantService.getRestaurantDetail(restaurantID)
call.enqueue(object: Callback<Any>{
override fun onResponse(call: Call<Any>, response: Response<Any>) {
if (response.isSuccessful) {
val gson = Gson()
val restaurantData = gson.fromJson(gson.toJson(response.body()), Restaurant::class.java)
} else {
val errorBody = response.errorBody() ?: return
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(errorBody.charStream(), type)
val errorMessage = errorResponse?.message ?: "Unknown Error"
}
}
})
don't forget to set the interface to be Any like this
interface RestaurantAPI {
#Headers("user-key: $USER_KEY_ZOMATO")
#GET("restaurant")
fun getRestaurantDetail(
#Query("res_id") id: Int
): Call<Any> // <---- set to Any like this
}
in my case, I have successful response and an error response. so I need to separate it like that.
but if you have 2 successful responses but it has different JSON then you need to perform null checking to restaurantData in my code above, if null then mapping it the other POJO.
You should use gson to convert json to an object
https://github.com/google/gson
Example
val gson = Gson();
val jsonInString = "{\"userId\":\"1\",\"userName\":\"Yasir\"}";
val user = gson.fromJson(jsonInString, User.class);
I have API response which contain object (graphic)
[
{
"code": 200,
"status": "OK",
"FirstDay": "2019-11-18",
"LastDay": "2019-11-24",
"graphic": {
"2019-11-23": [
{
"godzinaStart": "08:30",
"godzinaStop": "10:00",
"przedmiot": "Matematyka dyskretna",
"sala": "32AK8",
"nauczyciel": "xxx",
"grupy": "1K131; 1K132; 1K133; 1K134; 1K135; 2K131",
"typ": "wykład"
},
],
"2019-11-24": [
{
"godzinaStart": "08:30",
"godzinaStop": "10:00",
"przedmiot": "Podstawy informatyki",
"sala": "308K",
"nauczyciel": "xxx",
"grupy": "1K131",
"typ": "laboratorium"
},
]
}
}
]
I have to parse this JSON to object in Kotlin. So i made class with parameters
class GraphicAPIResponse(
var code: Int,
var status: String,
var errorMessage: String = "",
var FirstDay: String = "",
var LastDay: String = "",
var graphic: JsonObject? = null OR var graphic: JsonArray<Any>? = null (I tried both)
)
I'm parsing data by this function
val responeAPI = Klaxon().parseArray<GraphicAPIResponse>(response)
When graphic is JsonObiect type appliaction throw error
I/System.out: ERROR -> Unable to instantiate JsonObject with parameters []
When graphic is JsonArray<Any> type, here's error
I/System.out: ERROR -> Unable to instantiate GraphicAPIResponse with parameters [LastDay: 2019-11-24, code: 200, status: OK, graphic: java.lang.Object#aef265a, FirstDay: 2019-11-18]
I'm trying to resolve the problem from 2 hours. Can someone help me please? :(
#EDIT
Thank You #Alexey Romanov
That help
Define a type for the nested object:
class Lesson(val godzinaStart: String, val godzinaStop: String, ...)
and use it in GraphicAPIResponse:
class GraphicAPIResponse(
var code: Int,
var status: String,
var errorMessage: String = "",
var FirstDay: String = "",
var LastDay: String = "",
var graphic: Map<String, Lesson> = mapOf()
)
(though honestly, I'd expect JsonObject to work as well)
I've tried to imitate the chosen answers in this sort of problem but I'm unsure why I'm failing to retrieving the URLs of the "attachments" in this.
What I'm after is a way to get every "post" and then grab every "attachment" image URL to save as a string. I've tried doing this on my emulator but it just stalls and runs forever. For certain reasons I am unable to use my real phone as a debugger too or else I would post a logcat.
One thing I am certain is that everything, minus the attachments, is coming in correctly. I've managed to get the posts downloading but cannot get anything thats nested. I'm newer to JSON so any help is very appreciated.
My Async:
// you can make this class as another java file so it will be separated from your main activity.
// https://www.codeofaninja.com/2013/11/android-json-parsing-tutorial.html
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
private ArrayList<RssFeedItem> tempArray = new ArrayList<RssFeedItem>();
final String TAG = "AsyncTaskParseJson";
private ProgressDialog progress;
// set your json string url here
String yourJsonStringUrl = "http://www.prindlepost.org/?json=tag_slug=java";
// contacts JSONArray
JSONArray dataJsonArr = null;
JSONArray imageURLArr = null;
#Override
protected void onPreExecute() {
progress = new ProgressDialog(getActivity());
progress.setTitle("Downloading Prindle's Posts");
progress.setMessage("This should just take a moment.");
progress.show();
}
#Override
protected String doInBackground(String... arg0)
{
try
{
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("posts");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++)
{
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
String type = c.getString("type");
String slug = c.getString("slug");
String title = c.getString("title");
String content = c.getString("content");
String author = c.getString("author");
//http://stackoverflow.com/questions/19748829/android-get-json-array-nested-in-array
JSONObject attachments = c.getJSONObject("attachments");
Log.d("attachment",""+attachments.getString("url"));
// show the values in our logcat
Log.e(TAG, "id: " + id
+ ", type: " + type
+ ", slug: " + slug
+ ", title: " + title
+ ", author: " + author
+ ", content: " + content + "\n\n");
tempArray.add(new RssFeedItem(title, content, "", 0, new Date(), author));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
The JSON: http://www.prindlepost.org/?json=tag_slug=java
{
status: "ok",
count: 10,
count_total: 334,
pages: 34,
posts: [
{
id: 4230,
type: "post",
slug: "crowdsourcing-justice",
url: "http://www.prindlepost.org/2015/06/crowdsourcing-justice/",
status: "publish",
title: "Crowdsourcing Justice",
title_plain: "Crowdsourcing Justice",
content: "<p>The video begins abruptly. Likely recorded on a phone, the footage is shaky and blurry, yet the subject is sickeningly unmistakeable: a crying infant being repeatedly and violently dunked into a bucket of water. First it is held by the arms, then upside down by one leg, then grasped by the face as an unidentified woman pulls it through the water. Near the end of the video, the infant falls silent, the only remaining audio the splashing of water and murmured conversation as the child is dunked again and again.</p> <div class="more-link-wrap wpb_button"> Read more</div> ",
excerpt: "<p>Facebook’s decision not to censor a video of child abuse poses questions of censorship, activism and online justice. </p> ",
date: "2015-06-09 14:00:19",
modified: "2015-06-10 09:53:36",
categories: [
{
id: 433,
slug: "crime-and-law",
title: "Crime and Law",
description: "",
parent: 63,
post_count: 14
},
{
id: 38,
slug: "ethics-news",
title: "Ethics News",
description: "",
parent: 0,
post_count: 153
},
{
id: 63,
slug: "society-ethics-news",
title: "Society",
description: "",
parent: 38,
post_count: 187
}
],
tags: [
{
id: 180,
slug: "abuse",
title: "abuse",
description: "",
post_count: 2
},
{
id: 481,
slug: "child-abuse",
title: "child abuse",
description: "",
post_count: 1
},
{
id: 482,
slug: "doxxing",
title: "doxxing",
description: "",
post_count: 1
},
{
id: 57,
slug: "facebook",
title: "Facebook",
description: "",
post_count: 4
},
{
id: 470,
slug: "internet",
title: "internet",
description: "",
post_count: 2
},
{
id: 130,
slug: "justice",
title: "justice",
description: "",
post_count: 2
},
{
id: 59,
slug: "social-media",
title: "social media",
description: "",
post_count: 4
}
],
author: {
id: 43,
slug: "connergordon_2016",
name: "Conner Gordon",
first_name: "Conner",
last_name: "Gordon",
nickname: "connergordon_2016",
url: "http://connergordon.tumblr.com",
description: "Conner is a web and social media intern at the Prindle Institute. A Political Science and English double major from Carmel, Indiana, Conner's ethical interests lie in memory studies, conflict analysis and the ethics of representation. He also has interests in literature, art and photography."
},
comments: [ ],
attachments: [
{
id: 4233,
url: "http://www.prindlepost.org/wp-content/uploads/2015/06/Screen-Shot-2015-06-09-at-11.48.59-AM.png",
slug: "screen-shot-2015-06-09-at-11-48-59-am",
title: "",
description: "",
caption: "Image credit: Screenshot from Youtube",
parent: 4230,
mime_type: "image/png",
images: [ ]
},
{
id: 4235,
url: "http://www.prindlepost.org/wp-content/uploads/2015/06/Screen-Shot-2015-06-09-at-11.48.59-AM1.png",
slug: "screen-shot-2015-06-09-at-11-48-59-am-2",
title: "",
description: "",
caption: "Image/Youtube",
parent: 4230,
mime_type: "image/png",
images: [ ]
}
],
I had the same problem. After a few days melting my brain, I tried using Google's GSON. It does all the parsing and thinking for you, and returns a nice little object with all the information from the JSON.
Here's the project link: https://github.com/google/gson
To use it, you have to instantiate a new Gson parser, like so
Gson gson = new Gson();
YourObject object = gson.fromJson(jsonString, YourObject.class);
And the YourObject class should look something like this:
public class YourObject{
int status;
int count;
String count_total;
...
Post[] posts;
}
Now you create a Post class with the fields predicted in your JSON:
public class Post{
int id;
String type;
String slug;
...
Category[] categories;
}
I think you can get an idea on how to set up your POJO's. Keep in mind that, if you are getting an array of objects as your base object in JSON, be sure to use YourObject[] instead of YourObject when calling gson.fromJson
Just a heads-up: If any of the Json elements have a null or an empty value, even though they are primarily an int in your YourObject class, it is best to declare them as String to avoid java.lang.NumberFormatException.
You can use the method getJSONArray()
I have two jsons
{
success: 1,
camera: [
{
productid: 18486,
productkey: 509,
categoryid: 85,
categorykey: 2,
productname: "Samsung",
productimage: "samsung.jpg",
price: "10900"
},
{
productid: 18458,
productkey: 508,
categoryid: 85,
categorykey: 2,
productname: "Nikon Coolpix L29",
productimage: "nikoncoolpix.jpg",
price: "4446"
}]
}
Second Json
{
success: 1,
mobile: [
{
productid: 9999,
productkey: 519,
categoryid: 852,
categorykey: 21,
productname: "Samsung grand",
productimage: "samsung.jpg",
price: "10900"
},
{
productid: 1858,
productkey: 58,
categoryid: 5,
categorykey: 12,
productname: "nokia",
productimage: "nokia.jpg",
price: "44462"
}]
}
I need to parse the json using GSON,I tried these code
Gson mJson = new Gson();
PDProduct mObj = (PDProduct) mJson.fromJson(jsonStr,PDProduct.class);
List<ProductObj> mobiles = null;
mobiles = mObj.mobiles;
for (int i = 0; i < mobiles.size(); i++) {
HashMap<String, String> ProductDetails = new HashMap<String,
String>();
ProductDetails.put(Tags.PRODUCTTAG_CATGORY_ID,
mobiles.get(i).categoryid);
ProductDetails.put(Tags.PRODUCTTAG_CATGORY_KEY,
mobiles.get(i).categorykey);
ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_ID,
mobiles.get(i).productid);
ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_KEY,
mobiles.get(i).productkey);
ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_NAME,
mobiles.get(i).productname);
ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_PIC,
mobiles.get(i).productimage);
ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_PRICE,
mobiles.get(i).price);
ProductDetail.add(ProductDetails);
}
public class PDProduct {
public String success = null;
public List<PDProductObj> mobiles = null;
}
For this I'm getting only mobile data,How could dynamically create key?
Is there any other method to parse this data?
Your two JSON objects are two different objects; one has a field camera, one has a field mobile.
If you're the one producing that JSON the easiest answer is: fix the JSON. Use one field name and include something in the objects to denote a category (or whatever that is).
If you can't fix the JSON, you can write a custom deserializer that deals with it by renaming the field:
class MyDeserializer implements JsonDeserializer<PDProduct> {
#Override
public PDProduct deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException
{
JsonObject jo = je.getAsJsonObject();
if (jo.has("camera"))
{
JsonElement e = jo.remove("camera");
jo.add("mobile", e);
}
return new Gson().fromJson(jo, type);
}
}
Then use it with Gson:
Gson gson =
new GsonBuilder()
.registerTypeAdapter(PDProduct.class, new MyDeserializer())
.create();
The simplest solution is to change the json.
Add a new level "product" to the json and let it have name and data.
For example:
{
success: 1,
product:
{
name:camera,
data: [
{
productid: 18486,
productkey: 509,
categoryid: 85,
categorykey: 2,
...
Change PDIProduct class to reflect this change.
If you can't change the json (because you are getting it from an external source), change the JSON object to this structure before using GSON.