I have a JSON encode array given bellow:-
{
"imager": [{
"title": "Guru",
"images": ["images\/6.png", "images\/androidIntro.png", "images\/barretr_Earth.png"]
}]
}
My Problem is that I want to fetch all the images from images array one by one so that I can show the images on imageview. my Main aim is to display Title only one time and show all the images related to the title, I have searched all over the internet and stackflow but I am unable to find the proper answer
could anyone help me solving this?
I am using Volley Libabry here is my code:-
url = "myurl";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("imager");
for (int i=0; i<jsonArray.length(); i++)
{
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String images = object.getString("images");
// what to do here now?? help me?
}
if I set
textview.setText(images);
output will be like this:-
["images\/6.png","images\/androidIntro.png","images\/barretr_Earth.png"]
but i want only
images/6.png
images/androidIntro.png
images/battetr_Earth.png
So that I can so these all images in imageview.
You have to handle the "images" tag as an array.
Instead of
String images = object.getString("images");
use
JsonArray images = object.getJSONArray("images");
for (int j=0; j<images.length(); j++) {
String image = images.getString(j)
// image will be
// j = 0 -> "images\/6.png"
// j = 1 -> "images\/androidIntro.png"
// j = 2 -> "images\/barretr_Earth.png"
}
Related
I'm new on JSON and i have some problems with json array. I don't know how to parse that "genres" array. At the moment it only works as an object but don't look good.
I want that "genres" to show Drama,Musical instead of ["Drama","Musical"]
JSON:
{
"status_message":"Query was successful",
"data":{
"movie_count":25241,
"movies":[
{
"id":25805,
"title":"Rock Around the World",
"year":1957,
"genres":[
"Biography",
"Drama",
"Music"
],
The parsing code:
ArrayList<Film> films = new ArrayList<>();
try {
JSONObject object = new JSONObject(jsonResponse);
JSONObject data = object.getJSONObject("data");
JSONArray movies = data.getJSONArray("movies");
for (int i = 0; i < movies.length(); i++) {
JSONObject details = movies.getJSONObject(i);
String title = details.getString("title");
int year = details.getInt("year");
String genres = details.getString("genres");
Film film = new Film(title, year, genres);
films.add(film);
}
App show like this app
it's a simple way :
String genres = "[\"Drama\",\"Musical\"]";
genres = genres.replace("[", "");
genres = genres.replace("]", "");
genres = genres.replace("\"", "");
Log.i("MY_TAG",genres); // Drama,Musical
test it and use it !
Hello below is my image ulrs thats comes from backend services. The images are separated with comma(,). Please let me know how can i separate each images and set them to imageView.
Below is the url
{"data":[{"title":"Manali","organizer":"SINGH","image":",
Img_5c3ff5642013e20190117_084929.jpg,
Img_5c3ff564204b720190117_085027.jpg,
Img_5c3ff564206abPhoto_1547695381805.png,
Img_5c3ff56420e2f20190117_084853.jpg,",
"date1":"21 Apr 2019",
"date2":"24 Jan 2019",
"time1":"10:30AM",
"time2":"2:00PM",
"address":"VASANT VIHAR D-BLOCK MARKET NEW DELHI",
"city":"Delhi",
"state":"Delhi",
"price":"7000"}
]
}
Thanks in advance
TextUtils.split(",", fullImageString); will give you the images in a string array, and depending on how to use them you can show the images fetching the values from what you created. It is not possible to show multiple images in one ImageView however you can show multiple images with GridView, ListView or RecyclerView that holds multiple ImageView references, with using an adapter and such. If these are image URLs, use this library to efficiently process each URL and display them on image views, while working with list adapters.
To extract the urls, you would do the following :
try {
JSONObject jsonObject = new JSONObject(resultFromWebservice);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject innerObj = jsonArray.getJSONObject(i);
String imgURLs = innerObj.getString("image");
ArrayList urlList= new ArrayList(Arrays.asList(imgURLs.split(",")));
for (int j = 0; j < urlList.size(); j++) {
Log.d("Extracted URLs", "myMethod: " + urlList.get(j));
// You can set the imageView urls from here
}
}
} catch (JSONException e) {
e.printStackTrace();
}
This is my json response.
"hits" : [ {
"recipe" : {
"uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_ef498d1ca2ecb7104aa72d516af211d8",
"label" : "A Potato Dish for Julia Recipe",
"image" : "https://www.edamam.com/web-img/9bd/9bd993aeafaf4a439a59b5689ea6153f.jpg",
i need to get this as my list view response, i tried to add this by many ways but still i am getting blank screen as respose.
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray hits = jsonObj.getJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
JSONObject h = hits.getJSONObject(i);
JSONObject recipe = h.getJSONObject("recipe");
String Uri = recipe.getString("uri");
String Label = recipe.getString("label");
String Image = recipe.getString("image");
HashMap<String, String> recipe_ = new HashMap<>();
recipe_.put("uri", Uri);
recipe_.put("label", Label);
recipe_.put("image", Image);
recipeList.add(recipe_);
This is my solution what i was finding to call json object details by first calling json array then its object.
I have this code from JSONParser.java
try{
JSONArray postArray = jsonObject.getJSONArray("posts");
// Go through each post
for (int i = 0; i < postArray.length(); i++) {
JSONObject postObject = postArray.getJSONObject(i);
Post post = new Post();
post.setCfs(postObject.getJSONObject("custom_fields").optString("entity", "N/A"));
that displays this
["Some text"]
I want it to display without [" and "]
My Json for that specific part is as below
"posts":[{
"date":"2016-02-10 10:28:42",
"categories":[{}],
"tags":[],
"author":{"name":"admin"},
"custom_fields":{
"ref_number":["ITB NUMBER: ITB\/002\/2016"],
"deadline":["26 February, 2016"],
"entity":["Refugees (xxx)"]
}
It seems that "entity" is array and you should try something like that:
post.setCfs(postObject.getJSONObject("custom_fields").optJSONArray("entity").getString(0));
In the below code, I can display the first returned JSON place name result from my Google Places URL. Rather than just the first item, I'd like to display the whole returned list of place names. What would be the easiest way of doing this? Can you point me to a tutorial, or give sample code?
HttpGet get = new HttpGet(bcURL.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
json = new JSONObject(data);
JSONArray timeline = json.getJSONArray("results");
JSONObject lastport = timeline.getJSONObject(0);
return lastport;
}else{
Toast.makeText(NewGPS3.this, "oops", Toast.LENGTH_SHORT);
return null;
}
The line JSONObject lastport = timeline.getJSONObject(0); shows the first name. I'd like to show a list rather than just the first item.
You are going to need to use a JSONArray object in order to get at the array. It should go something like this:
JSONObject entireObject = new JSONObject("your JSON string here");
JSONObject mainObject = entireObject.getJSONObject("photos"); // main object in entire object
JSONArray arrayObject = mainObject.getJSONArray("photo"); // array object in the main object
You can then iterate through this array just like any other:
for (int i = 0; i < arrayObject.length(); i++) {
JSONObject photoData = arrayObject.getJSONObject(i);
String yourString = photoData.getString("id");
}
I got some of this out of Pro Android Media. There is a really good JSON api call example in this book. You can probably get the source code for free, too.
http://www.amazon.com/Pro-Android-Media-Developing-Smartphones/dp/1430232676/ref=sr_1_1?ie=UTF8&qid=1343062690&sr=8-1&keywords=android+media
for(int i = 0; i < timeline.size(); i++){
JSONObject lastport = timeline.getJSONObject(i);
//Save the item to a ArrayList and use that list for your ListAdapter.
googlePlaces.add(lastPort);
}