JSONException while using last.fm API in android - android

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");
}

Related

How to get some string from JsonObject?

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"));

Getting json from json array starting from array

[ {"serviceData": [ {"id": "1","service_name": "Plumber","act_stat": "1"},]}]
how to get this json Structure
String jsonString;
JSONArray jsonArray= new JSONArray(jsonString;);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsono= (JSONObject) jsonArray.get(i);
JSONArray ServiceArray= (JSONArray ) jsono.get(i);
for(int j=0;j<ServiceArray.length();j++)
{
String id=jsonobject.getString("id");
String service_name=jsonobject.getString("service_name");
String act_stat=jsonobject.getString("act_stat");
}
}
You can refer this post : How to Parse the JSON String Android for more details
First of all get JSONObject from JSONArray and then get particular field from that JSONObject like below :
JSONArray array;
for(int n = 0; n < array.length(); n++)
{
JSONObject object = array.getJSONObject(n);
String id = object .getString("id");
String service_name = object .getString("service_name");
String act_stat = object .getString("act_stat");
}
First of all get JSONObject from JSONArray and then get particular field from that JSONObject like below :
JSONObject jsonObj = new JSONObject(jsonStr);
SONArray jArray = jsonObj.getJSONArray("serviceData");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String id = jObject.getString("id");
String service_name = jObject.getString("service_name");
String act_stat = jObject.getString("act_stat");
}

How to parse this type of json array in android?

I have know to parser JSON array in single array but how to pass multiple JSON array and set it value to require
{"scode":"200","all_menu":[{"app_menu_id":"67","app_menu_name":"Demograpics","all_sub_menu":[{"app_menu_id":"67","app_sub_menu_id":"47","app_sub_menu_name":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/8451504072003.jpg"}],"sub_menu":"true"},{"app_menu_id":"68","app_menu_name":"Lyrics","all_sub_menu":[{"app_menu_id":"68","app_sub_menu_id":"48","app_sub_menu_name":"Music","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4681504072092.jpg"}],"sub_menu":"true"},{"app_menu_id":"69","app_menu_name":"Adult","all_sub_menu":[{"app_menu_id":"69","app_sub_menu_id":"49","app_sub_menu_name":"Double
Meaning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/9931504072151.jpg"}],"sub_menu":"true"},{"app_menu_id":"70","app_menu_name":"Emotions","all_sub_menu":[{"app_menu_id":"70","app_sub_menu_id":"50","app_sub_menu_name":"Love","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/7611504072164.jpg"}],"sub_menu":"true"},{"app_menu_id":"71","app_menu_name":"Wishes","all_sub_menu":[{"app_menu_id":"71","app_sub_menu_id":"51","app_sub_menu_name":"Good
Morning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/5171504072183.jpg"}],"sub_menu":"true"},{"app_menu_id":"72","app_menu_name":"Among Friend","all_sub_menu":[{"app_menu_id":"72","app_sub_menu_id":"52","app_sub_menu_name":"Friendship","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4411504072205.jpg"}],"sub_menu":"true"},{"app_menu_id":"73","app_menu_name":"Jokes","all_sub_menu":[{"app_menu_id":"73","app_sub_menu_id":"53","app_sub_menu_name":"Santa
Banta","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4331504072225.jpg"}],"sub_menu":"true"},{"app_menu_id":"74","app_menu_name":"Featured","all_sub_menu":[{"app_menu_id":"74","app_sub_menu_id":"54","app_sub_menu_name":"Ganpati
Bappa","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4771504072247.jpg"}],"sub_menu":"true"}]}
Suppose "response" is your JSONResponse
JSONObject jsonObject = new JSONObject(response);// This is used to get jsonObject from response
String sCode=jsonObject.optString("scode"); // This is how you can parse string from jsonObject
JSONArray allmenuArray=jsonObject.optJSONArray("all_menu"); //This is how you can parse JsonArray from jsonObject
for(int i=0;i<allmenuArray.length();i++){
JSONObject objectJson=allmenuArray.optJSONObject(i);//This is how you can parse jsonObject from jsonArray
}
Like This you can parse all your jsonObject and jsonarray. Just follow these steps you can easily parse your full JSONResponse
Try this.
try {
JSONObject jsonObject = new JSONObject("JSONResponse");
String scode = jsonObject.optString("scode");
JSONArray allmenuArray = jsonObject.optJSONArray("all_menu");
for (int i = 0; i < allmenuArray.length(); i++) {
JSONObject objectJson = allmenuArray.optJSONObject(i);
boolean sub_menu = objectJson.getBoolean("sub_menu");
String app_menu_id = objectJson.getString("app_menu_id");
String app_menu_name = objectJson.getString("app_menu_name");
JSONArray all_sub_menu = objectJson.getJSONArray("all_sub_menu");
for (int j = 0; j < all_sub_menu.length(); j++) {
JSONObject data = allmenuArray.optJSONObject(j);
Log.e("app_menu_id", data.getString("app_menu_id"));
Log.e("app_sub_menu_id", data.getString("app_sub_menu_id"));
Log.e("app_sub_menu_name", data.getString("app_sub_menu_name"));
Log.e("app_sub_menu_image", data.getString("app_sub_menu_image"));
}
}
} catch (JSONException e) {
Log.e("ERROr", e.toString());
}

org.json.JSONException: No value

org.json.JSONException: No value in array
StringRequest req = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
try {
questionsList = new ArrayList<QuestionsBean>();
JSONObject jsonObject = new JSONObject(s);
JSONArray question = jsonObject.getJSONArray("questions");
for (int i = 0; i < question.length(); i++) {
JSONObject x = question.getJSONObject(i);
QuestionsBean u = new QuestionsBean();
u.setDate(x.getString("date"));
u.setQuestion(x.getString("question_text"));
JSONObject bject = new JSONObject();
JSONArray answers = bject.getJSONArray("answers");
for (int j = 0; j < answers.length(); j++) {
JSONObject xx = answers.getJSONObject(i);
u.setAnswer_body(xx.getString("Answer_body"));
}
questionsList.add(u);
You are not calling getJSONArray on the correct JSONObject. Use
JSONArray answers = x.getJSONArray("answers");
Also, since your JSONArray could be null, use optJSONArray to avoid the exception and check if it's null before continuing.
Something like
JSONArray answers = x.optJSONArray("answers");
if (answers != null) {
// for loop
}
JSONObject bject = new JSONObject();
JSONArray answers = bject.getJSONArray("answers");
You constructed here an empty JSONObject, so it is normal what it cannot find 'answers' in it.

Json get JsonArray from JsonArray

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.

Categories

Resources