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();
}
}
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"));
For post method in android
I have to send array of objects.
I am retrieving elements from an arrayList and putting that retrieved element in JSONObject and than putting that in JSONobject in JSONArray.
JSONArray ccArray = new JSONArray();
{
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
for (int i = 0; i < ccArrayList.size(); i++) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}
} else {
object.put("", "");
}
}
When there are more than 2 or more than 2 elements in arraylist it is adding the last element in ccrray as many times as their are elements in the list.
output :
"cc":[{"emailId":"f#j.com"},{"emailId":"f#j.com"}]
Change code like this
JSONObject obj = new JSONObject();
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
} else {
object.put("", "");
}
}
obj.put("cc",ccArray);
Code:-
ArrayList<String> ccArrayList = new ArrayList<String>();
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
JSONArray ccArray = new JSONArray();
if(ccArrayList.size()>0){
for(int i=0;i<ccArrayList.size();i++){
JSONObject object = new JSONObject();
try {
object.put("emailId", ccArrayList.get(i));
} catch (JSONException e) {
e.printStackTrace();
}
ccArray.put(object);
}
}
Output:-
ccArray: [{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"}]
That's because you declared JSONObject object = new JSONObject(); outside the for loop, so each time it loops the object adds a new JSONObject and also does the array. Try keeping it within the loop:
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}
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 want to know how do I access the values of this JSON in Android:
{
"dados": [{
"Id": 3,
"IdChamado": 3,
"Chamado": "value",
"Solicitante": "value",
"Acao": "",
"ItemDeCatalogo": "Mobile | Instalação",
"InicioPrevisto": "06/01/2017 08:11:00",
"TerminoPrevisto": "06/01/2017 08:22:00"
}, {
"Id": 4,
"IdChamado": 4,
"Chamado": "value",
"Solicitante": "value",
"Acao": "",
"ItemDeCatalogo": "value",
"InicioPrevisto": "06/01/2017 08:11:34",
"TerminoPrevisto": "06/01/2017 08:11:34"
}],
"success": true,
"erroAplicacao": false
}
I need to access the values "IdChamado", "chamado", "Solicitante", for example. I've seen nested arrays answers but with jsonObjects having an actual name, like this .
PS: I'm sorry I forgot to post my codes:
//Method called when the doInBack is complete
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jArray = jsonObject.getJSONArray("dados");
Log.i("***2nd JSON ITSELF***", result);
for (int i=0; i<jArray.length(); i++) {
JSONObject jsonPart = jArray.getJSONObject(i);
int id = jsonPart.getInt("id");
Log.i("***id***", String.valueOf(id));
String chamado = jsonPart.getString("Chamado");
Log.i("***Chamado***", chamado);
String solicitante = jsonPart.getString("solicitante");
Log.i("***Solicitante***", solicitante);
String itemDeCatalogo = jsonPart.getString("itemDeCatalogo");
Log.i("***Item de Catalogo***", itemDeCatalogo);
}
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
}// END POST EXECUTE
[SOLVED]: Thank you so much people, you are the reason I like to code (Not be afraid of asking stupid questions). It all worked well with the codes you sent as answer. I thought it would be more complicated. xD
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonobject = new JSONObject(result);
JSONArray jsonarray = jsonobject.getJSONArray("dados");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jobject = jsonarray.getJSONObject(i);
String idChamado = jobject.getString("IdChamado");
String solicitante = jobject.getString("Solicitante");
Log.i("**id**", idChamado);
Log.i("**solicitante**", solicitante);
}
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
}// END POST EXECUTE
JSONObject jsonobject = new JSONObject("your JSON String here");
JSONArray jsonarray = jsonobject.getJSONArray("dados");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String IdChamado = jsonobject.getString("IdChamado");
String Solicitante = jsonobject.getString("Solicitante");
}
please try this
try this:
try{
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("dados");
for(int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String IdChamado = object.getString("IdChamado");
String Chamado = object.getString("Chamado");
//rest of the strings..
}
}
catch (JSONException e){
e.printStackTrace();
}
Try this
for (int i=0; i<jArray.length(); i++) {
JSONObject jsonobject = jArray.getJSONObject(i);
int IdChamado = jsonobject.getInt("IdChamado"); //idchamado here
String chamado = jsonobject.getString("Chamado");
String solicitante = jsonobject.getString("Solicitante");
}
Use the correct keys while opting any data
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jArray = jsonObject.getJSONArray("dados");
Log.i("***2nd JSON ITSELF***", result);
for (int i=0; i<jArray.length(); i++) {
JSONObject jsonObject = new JSONObject(result);
JSONArray jArray = jsonObject.optJSONArray("dados");
Log.i("***2nd JSON ITSELF***", result);
for (int i=0; i<jArray.length(); i++) {
JSONObject jsonPart = jArray.optJSONObject(i);
int id = jsonPart.optInt("Id");
Log.i("***id***", String.valueOf(id));
String chamado = jsonPart.optString("Chamado");
Log.i("***Chamado***", chamado);
String solicitante = jsonPart.optString("Solicitante");
Log.i("***Solicitante***", solicitante);
String itemDeCatalogo = jsonPart.optString("ItemDeCatalogo");
Log.i("***Item de Catalogo***", itemDeCatalogo);
}
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
}
I wrote a library for parsing and generating JSON in Android
http://github.com/amirdew/JSON
for your sample:
JSON jsonData = new JSON(jsonString);
//access IdChamado of first item:
int IdChamado = jsonData.key("dados").index(0).key("IdChamado").intValue();
//in loop:
JSON dadosList = jsonData.key("dados");
for(int i=0; i<dadosList.count(); i++){
int IdChamado = dadosList.index(i).key("IdChamado").intValue();
String Chamado = dadosList.index(i).key("Chamado").stringValue();
}
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.