I would like to view JsonArray Response and view with Table Layout in android.
Json Response is like this!
[{"id":1,"name":"hot drink"},{"id":2,"name":"cold drink"},{"id":3,"name":"myanmar food"},{"id":4,"name":"thai food"}]
For My Result XML file , I wanna See Like this with TableLayout
ID Name
1 Hot Drink
2 Cold Drink
3 Food
String[] id,name;
JSONArray jsonarrayobj = new JSONArray(yourjsonvalue);
id = new String[jsonarrayobj.length()];
name = new String[jsonarrayobj.length()];
for(int i=0;i<jsonarrayobj.length();i++){
JSONObject jobj = jsonarrayobj.getJSONObject(i);
id[i] = jobj.getString("id");
name[i] = jobj.getString("name");
}
Now you'll have the values in the id and name arrays.
Now with the help of this you can get the view as you said.
Related
I am A beginner at the android studio. I want to parse a JSON object and I want to display it like this.
String mNames[] = {"name1", "name2", "name3"};
How Can I do this.
You can use JSONObject and JSONArray as displayed in the following example:
{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");
int size = the_json_array.length();
ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = the_json_array.getJSONObject(i);
//Blah blah blah...
arrays.add(another_json_object);
}
//Finally
JSONObject[] jsons = new JSONObject[arrays.size()];
String[] blah = arrays.toArray(jsons);
(Taken from How to parse a JSON and turn its values into an Array?)
Also, just for your information, this process has nothing to do with Android Studio itself, rather with Java and JSON ^^
I used to have a response string that I converted to JSONObject in this way:
JSONObject obj_temp = new JSONObject(response);
Then, I added to that string some modifications. At first, the index 'TripDetails' just had 1 trip with it details (trip information, passenger information and driver information). Now, with the new modifications, 'TripDetails' is an array of trips. Each index has the same information for each trip (trip information, passenger information and driver information). But now, with this new string, Android Studio gives me this error:
of type org.json.JSONArray cannot be converted to JSONObject
Can a string be casted to JSONObject if it has this format? It's a valid JSON string. Here's the complete JSON: https://paste2.org/VemkLNMJ
It is because you made your json object an array (I don't see why since you have just 1 element). Anyway you can probably do something like this:
JSONArray arr = new JSONArray(response);
JSONObject obj_temp = arr.getJSONObject(0);
your response starts with Array and you trying to convert it into Object.
[ represents json array node
{ represents json object node
JSONArray obj_temp = new JSONArray(response);
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = obj_temp.getJSONObject(i);
String id = jsonobject.getString("id");
String title = jsonobject.getString("title");
String company = jsonobject.getString("company");
String category = jsonobject.getString("category");}
i my app in MainActivity View a execute a task that give me a json result.
I would like to know if is possibile to add dynamically a fab button into layout of other view in the number of json result.
Example if my json result have 3 items add 3 fab button to other view layout
This is my code about json call:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("businesses");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(0);
//String id = c.getString("id");
String name = c.getString("name");
String phone = c.getString("phone");
Taxi_Number = "tel:"+phone;
}
There is some example to do this or is not possible?
Thanks
I am new to android and JSON parsing. Here i have my json response that i get. Now i have several such response in my jsonarray. What i would like to know is how do i fetch a single value from this response.
i.e how can i fetch only "id" from these response:
{"id":"c200","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"ravi#gmail.com","name":"Ravi Tamada"}
{"id":"c201","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"johnny_depp#gmail.com","name":"Johnny Depp"}
I want only id of both these respones.
My code is
JSONObject jobject = jparse.getJSONFromUrl(url);
contacts = jobject.getJSONArray(TAG_CONTACTS);
for(int i = 0 ; i < contacts.length() ; i++)
{
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
Log.i("TAG", "STRING VALUE:" + contacts.getString(i));
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
}
If you dont want all id then remove for loop and just put
JSONObject j = contacts.getJSONObject(0);
String id = j.getString("id");
it will give the first object id that is only : c200
You got response from server in as json.
You can get only id as shown below code.
JSONObject json= new JSONObject(response);//response= your json string.
String id = json.getString("id");//id = param in your response
I have a little issue with parsing JSON String which I get from a web server. So my JSON looks something like this :
{
..........
"statistics":{
"660":{
"param_id":660,
"cat_id":2,
"param_title":"Number",
"param_value":"26",
"value_type":"singleline",
"elem_order":"1"}
,
"662":{
"param_id":662,
"cat_id":2,
"param_title":"Name",
"param_value":"Dani",
"value_type":"singleline",
"elem_order":"2"
}
// --||--
}
}
So I get a JSONObject statisics and I want to get JSONObjects from statistics, but the problem is that their name is different everytime.So I can't just do json.getJSONObject("660");. So any suggestions how can I do that?
You can do something like this :
if(jsonObj.has("statistics")){
Iterator<Object> keys = stats.keys();
while(keys.hasNext()){
String key = (String) keys.next();
JSONObject obj = new JSONObject();
obj = stats.getJSONObject(key);
// get JSON
}// end while
}//end if
use JSONObject.keys() to get key iterator, then getJsonObject( key) to get object.
Try below code-
JSONArray nameArray = statisics.names();
JSONArray valArray = statisics.toJSONArray(nameArray);
where names() method returns array of names and it is stored in nameArray and valArray contains array of values corresponding to valArray.
You can iterate using for loop over these array to get values.
use like this
JSONObject jsonOnb = json.getJSONObject("statistics") ;
JSONObject pagesObj = jsonOnb.getJSONObject(jsonOnb.names().getString(0));
Log.i("pageid", "pageid= " +pagesObj.get("param_id"));
Log.i("title", "title= " +pagesObj.get("cat_id"));
..............