How can i get data from array in array retrofit2 - android

Please take this reference
want to get appointment arraylist to inflate listview, i reached to call_detail but how can i get appointment array with retrofit2 gsonConverter
{
"patient":{},
"medicine":[
],
"call_detail":[
{
"created_on":"2016-01-22 06:06:00",
"call_type":"",
"rbs":"10",
"temp":"",
"provisional_diagnosis":"yes",
"follow_up_call_schedule":"",
"follow_up_call_date":"1970-01-01",
"reports":[
],
"medicalreference":[
],
"services_comment":"",
"health_consultation":"",
"appointment":[
{
"doctor_id":"28",
"clinic":"",
"hospital":"Shwe Nyaung Pin specialist ",
"name":"Dr.Nyein Mon Yu",
"app_date":"2016-01-21",
"app_time":"1:00 AM TO 1:00 AM"
}
]
}
],
"status":"true"
}

Try this..
try {
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("call_detail");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObjectCall=jsonArray.getJSONObject(i);
JSONArray jsonArrayAppointment=jsonObjectCall.getJSONArray("appointment");
for (int j=0;j<jsonArrayAppointment.length();j++)
{
JSONObject jsonObjectApp=jsonArrayAppointment.getJSONObject(j);
String doctor_id=jsonObjectApp.getString("doctor_id");
String clinic=jsonObjectApp.getString("clinic");
String hospital=jsonObjectApp.getString("hospital");
String name=jsonObjectApp.getString("name");
String app_date=jsonObjectApp.getString("app_date");
String app_time=jsonObjectApp.getString("app_time");
System.out.println("doctor_id"+doctor_id);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

To get appointment array try below code. Here jsonString is your given json in question:
try {
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray appointmentArray=jsonObject.getJSONArray("call_detail").getJSONObject(0).getJSONArray("appointment");
Log.d("appointmentArray",appointmentArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}

Related

Android : How to access a JSONObject

I'm new to Android and I have tried so many options to access the JSONObject which returns from an API call but I couldn't succeed as any of the solutions i looked for didn't work for me.
What i want is to access the JSONObject and keep the Id & Name in a Array. And then populate the Names in a AutoCompleteTextView. How do i properly access the JSONObject. Please help me with this. I'm stuck on this for more than a day.
Following is my Code handling the JSONObject.
#Override
public void processFinish(JSONObject output) {
Toast.makeText(MainActivity.this,"ProcessFinish",Toast.LENGTH_SHORT).show();
allStations = output;
if(output != null){
Toast.makeText(MainActivity.this,output.toString(),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this," Connection Failed!",Toast.LENGTH_SHORT).show();
}
}
Following is a sample output of my JSONObject
{
"SUCCESS": true,
"MESSAGE": "Found 398 Results!",
"NOFRESULTS": 3,
"RESULTS": {
"stationList": [
{
"stationCode": "ABN",
"stationID": 3,
"stationName": "ABLA"
},
{
"stationCode": "ADLA",
"stationID": 410,
"stationName": "ADLA"
},
{
"stationCode": "ANM",
"stationID": 11,
"stationName": "AHAMA"
}]
},
"STATUSCODE": "2000"
}
try this
try {
JSONObject obj= output.getJSONObject("RESULTS");
JSONArray dataArray= obj.getJSONArray(“stationList“);
for(int i=0;i<dataArray.length();i++)
{
JSONObject object1=dataArray.getJSONObject(i);
Strind id = object1.getString("stationID");
}
} catch (JSONException e) {
e.printStackTrace();
}
In This code output is your JSONObject result
try this
try {
JSONObject jsonObject = new JSONObject("response");
boolean status= jsonObject.getBoolean("SUCCESS");
String MESSAGE= jsonObject.getString("MESSAGE");
String NOFRESULTS= jsonObject.getString("NOFRESULTS");
String STATUSCODE= jsonObject.getString("STATUSCODE");
JSONObject obj=jsonObject.getJSONObject("RESULTS");
JSONArray jsonarray = obj.optJSONArray("stationList");
for (int i = 0; i < jsonarray.length(); i++){
JSONObject json_data = jsonarray.getJSONObject(i);
Log.e("stationCode",json_data.getString("stationCode"));
Log.e("stationID",json_data.getString("stationID"));
Log.e("stationName",json_data.getString("stationName"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Have you tried using a JSON Array? For example you could use this method for storage:
JSONObject wgroup = new JSONObject(); //FINAL json object
try { //put initial data
wgroup.put("id", "2");
wgroup.put("user", "someone");
wgroup.put("stime", "2017-02-06 16:30:13");
wgroup.put("etime", "2017-02-06 19:30:13");
wgroup.put("real_dur", 3600);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray jsonArray1 = new JSONArray(); //Create an array to store ALL Variables
for (int y=0; y< your_array.length ; y++ ){ //loop through your information array
JSONObject output = new JSONObject(); //CREATE a json object to put 1 workout
try {
wgroup.put("id", "2");
wgroup.put("name", "sam");
wgroup.put("age", "3");
wgroup.put("gender", "male");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray jsonArray2 = new JSONArray(); //CREATE a json array to put 1 array
jsonArray1.put(output); //insert this OBJECT into the ARRAY
}
wgroup.put(jsonArray1);//insert the workouts ARRAY into the original object

I want to read this JSONArray,little confused..please guide me

How to read the images and its corresponding positions
"images": [
[
{
"images_url": "http://provenlogic.info/tinder_web/public/uploads/e9275d47cf5efd929794caafc50e957982c47582.jpg",
"position": "1"
},
{
"images_url": "http://provenlogic.info/tinder_web/public/uploads/c374561da8583a77b4d21ee4b06f30d1a3fac4bb.jpg",
"position": "3"
}
]
]
try {
JSONArray jsonArray = rootObject.getJSONArray("images");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String imageUrl=jsonObject.getString("images_url");
String position=jsonObject.getString("position");
}
} catch (Exception e) {
e.printStackTrace();
}
Happy Codding!!!
A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].
JSONArray jArray = json.getJSONArray("images").get;
for(int i = 0; i < jArray.length;i++)
{
JSONObject jObj = jsonArray.getJSONObject(i);
String imageUrl = jObj.getString("images_url");
String position = jObj.getString("position");
}
You will need to wrap the whole JSON Handling inside a try/catch block. You might want to try http://jsonlint.com/, an online json validator and https://jsonformatter.curiousconcept.com/, an online json formatter to understand easily!
Cheers!
First of all get the JsonArray by using array name "images"
and get each Json object .
try {
JSONArray jsonArray = new JSONArray("result");
for(int i=0;i<jsonArray.length();i++){
JSONArray jsonArray1=jsonArray.getJSONArray(i);
for(int j=0;j<jsonArray1.length();j++){
JSONObject jsonObject=jsonArray1.getJSONObject(j);
String imageurl=jsonObject.getString("images_url");
String position=jsonObject.getString("position");
}
}
}catch (Exception e){
e.printStackTrace();
}

Issue in parsing irregualr json format in android

I want to parse following JSON
{
"fail": true,
"errors": {
"arrival_date": ["The arrival date should be greater than or equal to departure date !!!"]
}
}
I want to get value of The arrival date should be greater than or equal to departure date !!!
I tried this code but not getting success
try {
org.json.JSONObject jsonObj = new org.json.JSONObject(PostParseGet.mStringresponse);
org.json.JSONObject c = jsonObj.getJSONObject("errors");
JSONArray contacts = c.getJSONArray("arrival_date");
Log.d("Errors",contacts.get(0).toString());
} catch (JSONException e) {
e.printStackTrace();
}
Any idea how can I get this value?
String data ="{ 'fail': 'true', 'errors': { 'arrival_date': ['The arrival date should be greater than or equal to departure date !!!']}}";
try {
JSONObject object = new JSONObject(data);
JSONObject error = object.getJSONObject("errors");
JSONArray array = error.getJSONArray("arrival_date");
String dataString = array.getString(0);
String dat = dataString;
}
catch(Exception e) {
e.printStackTrace();
}
Try below Code
try {
JSONObject jObject=new JSONObject(PostParseGet.mStringresponse);
JSONObject errorObject=jObject.optJSONObject("errors");
JSONArray arraiValArray=errorObject.optJSONArray("arrival_date");
if(arraiValArray.length()>0)
{
for (int i = 0; i < arraiValArray.length(); i++) {
Log.i("TAG", "String:"+arraiValArray.optString(i));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

JSON array retrieve value by name

public void onResponse( String response ){
JSONArray jsonArray ;
try{
JSONObject jsonObject = jsonArray.getJSONObject(1);
String result = jsonObject.getString("name");
t.setText(result);
}
catch(JSONException e){
e.printStackTrace();
}
}
and my json file is
[
{
"0":"1",
"1":"Adarsh",
"id":"1",
"name":"Adarsh"
},
{
"0":"2",
"1":"Asif",
"id":"2",
"name":"Asif"
},
{
"0":"3",
"1":"Baba",
"id":"3",
"name":"Baba"
},
{
"0":"4",
"1":"Beeta",
"id":"4",
"name":"Beeta"
}
]
In my application it doesn't retrieve the value for "name" instead showing error toast.
jsonArray has not been initialised
jsonArray = new JSONArray("yourjson");
String name = jsonArray.getJSONObject(1).getString("name");
First change your string response to json response like this:
JsonArray jarray=new JsonArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(1);
String result = jsonObject.getString("name");
t.setText(result);
Here you are trying to fetch data from string as per your comment so need to convert in to json first like this
public void onResponse( String response ){
try{
JSONArray jsonArray= new JSONArray(response);
Log.d("JsonArray", jsonArray.toString());
JSONObject jsonObject = jsonArray.getJSONObject(1);
String result = jsonObject.getString("name");
t.setText(result);
}
catch (Throwable t) {
Log.e("ERROR", "Wrong Json format");
}
catch(JSONException e){
e.printStackTrace();
}
}

json Parsing trouble in android

I get a problem when I parse this text as JSON. First, I created jsonobject from an URL. I can't show data when debugging, can anyone help me?
JSON:
[
{
"Tarih":"21.12.2015",
"Imsak":"05:51",
"Gunes":"07:22",
"Ogle":"12:18",
"Ikindi":"14:39",
"Aksam":"17:02",
"Yatsi":"18:26",
"Kible":"09:41"
}
]
I tried this code to solve it, but the JSON wasn't correctly parsed:
Java Code:
jsonobject = JSONfunctions.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("d");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
'[' indicates a JsonArray whereas '{' indicates a JsonObject. You have assigned the result to the JsonObject. Instead of that assign it to JsonArray.
Also I don't see any key as "d" in you response.
Try this :
jsonarray = JSONfunctions
.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
But again please note your response does not contain this keys 'SehirId', 'population' and 'SehirAdi'
Your response is JsonArray not a JsonObject.
Now you have two ways to solve this problem.
change your getJSONfromURL() to return JsonArray
change response from server to return JsonObject instead of JsonArray
after doing above changes you must need to parse it according to your response.
Note : You have catch block available, so please try to check error in logcat and search on google related to that error.
try this solution :
try {
JSONArray jsonArray = new JSONArray("Your Response");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
Iterator keyNames = jsonObject.keys();
while (keyNames.hasNext()) {
String keyName = (String) keyNames.next();
String keyValue = jsonObject.getString(keyName);
}
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources