Getting json from json array starting from array - android

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

Related

JSONException while using last.fm API in 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");
}

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

Difficult to identify json data

I have json data as mentioned below.
{
"data":[
{
"Products":{
"id":"86",
"pname":"mi4",
"pcat":"9",
"subcat":"8",
"seccat":"0",
"oproduct":"1",
"pdetails":"Good phone",
"pprice":"10000",
"pdiscount":"10",
"qty":"1",
"qtytype":"GM",
"dcharge":"40",
"pimage":null,
"sname":"Easydeal",
"sid":"1100",
"size":"",
"pincode":""
}
}
]
}
I can identify array as getJSONArray("datas"). But I want to get pname and sname values.
Just reach the object
JSONObject resp=new JSONObject("response");
JSONArray data=resp.getJSONArray("data");
now if you want to get object at a particular index(say '0')
JSONObject objAt0=data.getJSONObject(0);
JSONObject products=objAt0.getJSONObject("products");
String pName=products.getString("pname");
you can similarly traverse the array
for(int i=0;i<data.lenght();i++){
JSONObject objAtI=data.getJSONObject(i);
JSONObject products=objAtI.getJSONObject("products");
String pName=products.getString("pname");
}
To get the to the key "Products" you should do:
JSONObject productsObject = YOUROBJECTNAME.getJSONArray("data").getJSONObject(0).getJSONObject("Products");
Then to get the values in productsObject you should do:
productsObject.getString("id");
productsObject.getString("pdetails");
And so on.
Try out the following code:
JSONObject object = new JSONObject(result);
JSONArray array = object.getJSONArray("data");
JSONObject object1 = array.getJSONObject(0);
JSONObject products = object1.getJSONObject("Products");
int id = object1.getInt("id");
String pname = object1.getString("pname");
This is how you get pname and sname:
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray;
try {
jsonArray = jsonObject.getJSONArray("data");
for(int counter = 0; counter <jsonArray.length(); counter++){
JSONObject jsonObject1 = jsonArray.getJSONObject(counter);
JSONObject products = jsonObject1.getJSONObject("Products");
String pname = products.getString("pname");
String sname = products.getString("sname");
}
} catch (JSONException e) {
e.printStackTrace();
}
PS: Poor JSON structure :)

org.json.JSONException: Find specific string from Json Object

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

Problems with getting values from JSON object

I'm trying to get data once user logged in successfully but I never get any of results, what I am doing is next:
// response is my request to server
JSONObject obj = new JSONObject(response);
Log.d("RESPONSE",obj.toString());
so in log I do see values, like:
04-19 11:28:16.729: D/RESPONSE(3162): {"data":[{"loses":3,"username":"benedict","level":1,"strength":15,"experience":null,"gold":10,"password":"benedict","intelligence":5,"agility":10,"wins":5}],"status":true}
but once I try to read username for example like this:
String username = obj.getString("username");
The code above ^ gives me nothing in my string..
Any help how I can retrieve data from JSONObject? Thanks!
That is because the username is present in the data object, which happens to be an JSONArray. Get the data array from the response object, traverse through each JSONObject in the array, and from each object, extract your username.
Something like this:-
JSONObject obj = new JSONObject(response);
JSONArray data = obj.getJSONArray("data");
for(int i=0;i<data.length();i++){
JSONObject eachData = data.getJSONObject(i);
System.out.println("Username= "+ eachData.getString("username"));
}
your field username is in array data. To access into this try :
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("data");
for(int i = 0; i < array.length(); ++i){
JSONObject data = array.getJSONObject(i);
String username = data.getString("username");
}
You need to first get JSONArray which is data :
JSONArray data = null;
data = json.getJSONArray("data");
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String username = c.getString("username");
}
You can get idea about parsing JSON from HERE
Try this...
try {
JSONObject object = new JSONObject(response);
JSONArray Jarray = object.getJSONArray("data");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject Jasonobject = Jarray.getJSONObject(i);
String loose= Jasonobject.getString("loses");
String username=Jasonobject.getString("username");
.......
........
}
} catch (JSONException e) {
Log.e("log_txt", "Error parsing data " + e.toString());
}

Categories

Resources