I have the following Json. link
I would like to get image_hall_list and image_place_list all url value.
I tried with the following code but no any result.
JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("image_place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = null;
try {
jo = ja.getJSONObject(i);
jsonurl.add(jo.getString("url"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
Try this:
JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = null;
try {
jo = ja.getJSONObject(i);
JSONArray imageHallList = jo.getJSONArray("image_hall_list");
for (int j = 0; j < imageHallList.length(); j++) {
JSONObject oneImageHallList = imageHallList.getJSONObject(j);
jsonurl.add(oneImageHallList.getString("url"));
}
JSONArray imagePlaceList = jo.getJSONArray("image_place_list");
for (int j = 0; j < imagePlaceList.length(); j++) {
JSONObject oneImagePlaceList = imagePlaceList.getJSONObject(j);
jsonurl.add(oneImagePlaceList.getString("url"));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
I would recommend some methods.
One to extract all the URLs for a given object.
public ArrayList<String> getURLs(JSONObject jo, String key) throws JSONException {
List<String> urls = new ArrayList<String>();
JSONArray arr = jo.getJSONArray(key);
for (int j = 0; j < arr.length(); j++) {
JSONObject innerObj = arr.getJSONObject(j);
urls.add(innerObj.getString("url"));
}
return urls;
}
Then, you can use that twice for the respective keys. You also need to first get "place_list" based if your result variable is directly from that link.
try {
JSONObject jsonResponse = new JSONObject(result);
JSONArray ja = jsonResponse.getJSONArray("place_list"); //get the array
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
jsonurl.addAll(getURLs(jo, "image_hall_list"));
jsonurl.addAll(getURLs(jo, "image_place_list"));
}
} catch (JSONException e1) {
e1.printStackTrace();
}
Use a nested for-loop. First grab the items before the image_hall_list and image_place_list, and then once you have the values stored, loop through image_hall_list, and image_place_list by getting that JSON object and loop through the elements in the JSON objects.
Related
I got from server response by retrofit, that is actually JsonObject(using Gson):
{"a": "a and its content 1", "b": [{"b_1": "string: b_1", "b_2": 2222}]}
so that I get it like this:
JsonObject jsonObject = response.body();
And then I can log it:
Log.d(TAG, jsonObject.get("a")+"");
// log: "a and its content 1"
question:
How can I log only "string: b_1"? (from "b_1": "string: b_1")
As it is in array [], hard to get it for me.
Try this one work for me,
try {
JSONObject jsonObject = response.body();
JSONArray jsonArray = jsonObject.getJSONArray("b");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String b_1 = jsonObj.getString("b_1");
int b_2 = jsonObj.getInt("b_2");
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
try{
JsonObject jsonObject = response.body();
JSONArray a = jsonObject.getJSONArray("b")
for (int i = 0; i < a.length(); i++) {
Log.d("Type", a.getString(i));
}
}catch(Exception e){
}
Try like below
JSONObject jObj = response.body();
JSONArray jsonArray = jObj.getJSONArray("b");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.d(TAG, jsonObject.getString("b_1")+"");
Log.d(TAG, jsonObject.getInt("b_2")+"");
}
try this
JSONObject jsonObject = new JSONObject(responseString);
String status = String.valueOf(jsonObject.get("a"));
JSON :
{"results":{"opensearch:Query":{"#text":"","role":"request","searchTerms":"Rose","startPage":"1"},"opensearch:totalResults":"102325","opensearch:startIndex":"0","opensearch:itemsPerPage":"1","artistmatches":{"artist":[{"name":"Guns N' Roses","listeners":"3198315","mbid":"eeb1195b-f213-4ce1-b28c-8565211f8e43","url":"https://www.last.fm/music/Guns+N%27+Roses","streamable":"0","image":[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"medium"},{"#text":"https://lastfm-img2.akamaized.net/i/u/174s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"large"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"extralarge"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"mega"}]}]},"#attr":{"for":"Rose"}}}
I am unable to fetch the artist details using this JSON.
I would like to know the format to use this JSON in Volley android.
Code:
final JsonArrayRequest jsonArrayRequest = new
JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
JSONObject obj1 = object.getJSONObject("results");
JSONObject obj2 = obj1.getJSONObject("artistmatches");
JSONArray m_jArry = obj2.getJSONArray("artist");
for (i = 0; i < m_jArry.length(); i++)
{
JSONObject obj4 = m_jArry.getJSONObject(i);
String name=obj4.getString("name");
String mbid=obj4.getString("mbid");
String url=obj4.getString("url");
}
You need to use JSONObject as Response rather than JSONArray because, you're receiving JSONObject from API.
Use like below:
JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//for (int i = 0; i < response.length(); i++) { Removed for loop
try {
JSONObject object = response; //We're using JSONObject from API here
JSONObject obj1 = object.getJSONObject("results");
JSONObject obj2 = obj1.getJSONObject("artistmatches");
JSONArray m_jArry = obj2.getJSONArray("artist");
for (i = 0; i < m_jArry.length(); i++)
{
JSONObject obj4 = m_jArry.getJSONObject(i);
String name=obj4.getString("name");
String mbid=obj4.getString("mbid");
String url=obj4.getString("url");
}
Try this....i have edited your jsonResponse
JSONObject jsonObject = new JSONObject(response);
JSONObject results = jsonObject.getJSONObject("results");
JSONObject opensearch = results.getJSONObject("opensearch:Query");
String text = opensearch.getString("#text");//after this get your all string of "opensearch:Query" object same way
String totalResults=results.getString("opensearch:totalResults");////after this get your all other string like "opensearch:startIndex" and "opensearch:itemsPerPage" of "results" object same way
JSONObject artistmatches = results.getJSONObject("artistmatches");
JSONArray artist = artistmatches.getJSONArray("artist");
JSONArray image = artist.getJSONArray("image");
for (int i = 0; i < artist.length(); i++) {
JSONObject allartist = artist.getJSONObject(i);
String name=artist.getString("name"); // same you can get all strings like "listeners","mbid".. of artist array
//by this you will get images array
for (int j = 0; j < image.length(); j++) {
JSONObject allimage = image.getJSONObject(i);
String imageText=allimage.getString("#text");
String size=allimage.getString("size");
}
Hi i want to post this a json array which has two json arrays of Integers in them to server using JsonObject
"job_experiences": [
{
"job_category": [Integer],
"experience_level": [Integer]
}
]
JsonArray list = new JsonArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list.add(selectedJobs.get(i));
}
JsonArray list2 = new JsonArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list2.add(selectedJobs.get(i));
}
JsonObject postData = new JsonObject();
postData.add("job_category", list);
postData.add("experience_level", list2);
Please use JSONObject instead of JsonObject and JSONArray instead of JsonArray,
Try below approach and check
JSONArray list = new JSONArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list.put(selectedJobs.get(i));
}
JSONArray list2 = new JSONArray();
for (int i = 0; i < selectedJobs.size(); i++) {
list2.put(selectedJobs.get(i));
}
JSONObject postData = new JSONObject();
try {
postData.put("job_category", list);
postData.put("experience_level", list2);
JSONArray job_experiences = new JSONArray();
job_experiences.put(postData);
JSONObject jsonObject_job_experiences = new JSONObject();
jsonObject_job_experiences.put("job_experiences", job_experiences);
Log.e("jsonObject_job_exp", jsonObject_job_experiences.toString());
} catch (JSONException e) {
e.printStackTrace();
}
and in the log you can see the final JSONObject
I am working in Android and finding json from internet that looks like this:
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
I want to find C21 that is in the A. See the json coming from request.
Can someone help me?
try {
JSONObject j=new JSONObject(data);
JSONArray c= null;
c = j.getJSONArray("This");
JSONObject item=c.getJSONObject(0);
JSONArray me=item.getJSONArray("me");
for(int pos=0;pos<me.length();pos++)
{
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
}
} catch (JSONException e1) {
e1.printStackTrace();
}
//hope this will help you and also check json is invalid or not ,you missed
// bracket of jsonarray "me".
You have missed THIS json array while parsing. First get that and from that json object and then get ME json array. Something like this
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("This");
JSONObject j1 = c.getJSONArray(0);
JSONArray d = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject item = c.getJSONObject(n);
System.out.println(item.getString("A"));
}
Try this:
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject person = (JSONObject) c.get(n );
String id = person.getString("A");
...
}
I am getting data from an array from server . Now I want to put the data received (name of the users ) into an string array . I do not know to put data into a string array.
code:-
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
JSONArray jArray = jo.getJSONArray("driver_details");
for (int i=0; i < jArray.length(); i++)
{
System.out.println("please print my name...for....");
try {
JSONObject jsob = jArray.getJSONObject(i);
// Pulling items from the array
mfname = jsob.getString("first_name");
}
}
You have several choices. One being using String[] or ArrayList();
JSONObject jo = new JSONObject(data);
Log.d("response", jo.toString(4));
JSONArray jArray = jo.getJSONArray("driver_details");
String[] names = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
System.out.println("please print my name...for....");
try {
JSONObject jsob = jArray.getJSONObject(i);
// Pulling items from the array
mfname = jsob.getString("first_name");
names[i] = mfname;
}
}
You can use ArrayList() too:
List<String> names= new ArrayList<String>();
....
names.add(mfname);
Try this..
For String array
String[] array_items = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
try{
JSONObject jsob = jArray.getJSONObject(i);
array_items[i] = jsob.getString("first_name");
} catch (JSONException e) {
e.printStackTrace();
}
}
For String ArrayList
ArrayList<String> arraylist_items = new ArrayList<String>();
for (int i=0; i < jArray.length(); i++)
{
try{
JSONObject jsob = jArray.getJSONObject(i);
arraylist_items.add(jsob.getString("first_name"));
}catch (JSONException e) {
e.printStackTrace();
}
}
Try something like below code:
For Arraylist:
JSONObject jo = new JSONObject(data);
JSONArray jArray = jo.getJSONArray("driver_details");
ArrayList<String> users = new ArrayList<String>();
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject jsob = jArray.getJSONObject(i);
String mfname = jsob.getString("first_name");
users.add(mfname);
}
catch (JSONException e) {
e.printStackTrace();
}
}
For String array:
JSONObject jo = new JSONObject(data);
JSONArray jArray = jo.getJSONArray("driver_details");
String[] users = new String[jArray.length()];
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject jsob = jArray.getJSONObject(i);
String mfname = jsob.getString("first_name");
users[i] = mfname;
}
catch (JSONException e) {
e.printStackTrace();
}
}