Reputation: 31
Given below is my JSON
response from web-service.
{"message":"success","data":[{"push_status":"1"}]}
I want to get the value of push_status from {"push_status":"1"}
.
This is my code:
public static VehicleDetails getPushNotificationstatus(String clientCode, String secretode) throws ClientProtocolException,
IOException, JSONException {
Log.d(WebServiceHelper.TAG, "getPushNotificationstatus==============>");
VehicleDetails vdetails = null;
String result;
ArrayList<VehicleDetails> SIArrayList = new ArrayList<VehicleDetails>();
JSONObject jObject = null;
try {
Log.d(WebServiceHelper.TAG, "WebserviceHelperOnLogin>>>>>>>>>>>");
vdetails = new VehicleDetails();
METHOD_NAME = "getPushNotifyStatus";
Log.d(WebServiceHelper.TAG, "URL>>>>>>>>" + URL + METHOD_NAME);
HttpPost request = new HttpPost(URL + METHOD_NAME);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("clientCode", clientCode));
postParameters.add(new BasicNameValuePair("secretCode", secretode));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(entity);
HttpResponse response = getThreadSafeClient().execute(request);
entityResponse = response.getEntity();
result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
// Log.i("FB", "result ::: " + result);
Log.d(TAG, "ResultPushStatus>>>>" + result);
jObject = new JSONObject(result);
vdetails.status_login = jObject.getString("message");
// Log.i("FB", "contact.status_login ::: " + contact.status_login);
if (vdetails.status_login.contentEquals("success")) {
jObject = new JSONObject(jObject.getString("data"));
vdetails.pushStatus = jObject.getString("push_status");
Log.d(TAG, "Push Status==================>" + vdetails.pushStatus);
} else if (vdetails.status_login.contentEquals("failed")) {
String Reason = jObject.getString("data").toString();
Log.d(WebServiceHelper.TAG, "Fail Reason>>>>>" + Reason);
vdetails.failReason = Reason;
}
} catch (Exception e) {
e.printStackTrace();
}
return vdetails;
}
public static VehicleDetails[] getAllVehicles(String clientCode, String
secretCode) throws ClientProtocolException, IOException, JSONException {
VehicleDetails[] vd = null;
String result = null;
VehicleDetails vdetails = null;
ArrayList<VehicleDetails> vehicleArrayList = new ArrayList<VehicleDetails>();
JSONObject jObject = null;
String loginUrl = "getAllVehicles";
try {
HttpPost request = new HttpPost(URL + loginUrl);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("clientCode", clientCode));
postParameters.add(new BasicNameValuePair("secretCode", secretCode));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);
HttpResponse response = getThreadSafeClient().execute(request);
entityResponse = response.getEntity();
result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
Log.d(TAG, "result>>" + result);
JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
VehicleDetails.status_login = object.getString("message");
if (VehicleDetails.status_login.contentEquals("success")) {
JSONArray array = object.getJSONArray("data");
vehicleArrayList.clear();
for (int i = 0; i < array.length(); i++) {
Log.d(TAG, "LiveTracking>>>>>>>>>>>");
JSONObject jObj = array.getJSONObject(i);
String vehicleId = jObj.getString("vehicle_id").toString();
String vehicleNumber = jObj.getString("vehicle_number").toString();
vdetails = new VehicleDetails();
vdetails.vehicleId = vehicleId;
vdetails.vehicleNo = vehicleNumber;
vehicleArrayList.add(vdetails);
}
vd = new VehicleDetails[vehicleArrayList.size()];
for (int x = 0; x < vehicleArrayList.size(); ++x) {
vd[x] = (VehicleDetails) vehicleArrayList.get(x);
}
} else if (VehicleDetails.status_ login.contentEquals("failed")){
JSONArray array = object.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject jObj = array.getJSONObject(i);
vdetails.failReason = jObj.getString("data").toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return vd;
}
This is my log show.
05-02 14:34:40.597: W/System.err(10261): org.json.JSONException: Value [{"push_status":"1"}] of type org.json.JSONArray cannot be converted to JSONObject
05-02 14:34:40.617: W/System.err(10261): at org.json.JSON.typeMismatch(JSON.java:107)
05-02 14:34:40.627: W/System.err(10261): at org.json.JSONObject.<init>(JSONObject.java:158)
05-02 14:34:40.627: W/System.err(10261): at org.json.JSONObject.<init>(JSONObject.java:171)
Whats the reason for this?please Help me
Upvotes: 0
Views: 255
Reputation: 8023
In the following line, 'data' returns a JSON Array not a String. (Since it is enclosed in [])
jObject = new JSONObject(jObject.getString("data"));
So you'll need to get the jsonArray first.
JSONArray jArray = jObject.getJSONArray("data");
Then get the first object from the array.
JSONObject dataObject = jArray.getJSONObject(0);
now fetch the String.
String pushStatus = dataObject.getString("push_status");
Upvotes: 1
Reputation: 1902
Response is not a JSON object ,it's a JSON array with one element.
JSONArray a = new JSONArray("[{your JSON code}]");
JSONObject = a.getJSONObject(1);
Upvotes: 1