org.json.JSONObject cannot be converted to JSONArray in android
You could try this:
JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("contacts");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
}
Value type org.json.JSONObject cannot be converted to JSONArray
You're trying to set the "timeline" value to a JSONArray
variable while that value would actually be a JSONObject
. Here's a way you can retrieve both the key and value:
JSONObject jsonObject = new JSONObject(response);
JSONObject subObject = jsonObject.getJSONObject("timeline");
// Separate the keys into a JSONArray
JSONArray keys = subObject.names();
// Retrieve the keys and values
for(int i=0; i < keys.length(); i++) {
String key = keys.getString(i);
int value = subObject.getInt(key);
Log.d("LOG_TAG", key + " : " + value);
}
JSONObject cannot be converted to JSONArray in android
productdata
is not a JSONArray but a JSONObject, so you have to change
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
to
JSONObject jsonObject1 = jsonObject.getJSONObjct("productdata");
org.json.JSONObject cannot be converted to JSONArray on my android project
Try this, it will loop through the object based on the keys:
try {
JSONObject lesMots = response.getJSONObject("lesMots");
Iterator<?> keys = lesMots.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
if (lesMots.get(key) instanceof JSONObject ) {
JSONObject obj = (JSONObject) lesMots.get(key);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e +"",Toast.LENGTH_LONG).show();
}
org.json.JSONObject cannot be converted to JSONArray
Its clear from error that you are trying to convert Json Object into Json array. That should not.
Here is the code to read your JSON response.
String json = "Assuming that here is your JSON response";
try {
JSONObject parentObject = new JSONObject(json);
JSONObject userDetails = parentObject.getJSONObject("user_details");
//And then read attributes like
String name = userDetails.getString("user_name");
String phone = userDetails.getString("user_phone");
String id = userDetails.getString("ref_id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Above code is for {"user_details":{"user_id":"1","user_name":"chand","user_phone":"9620085675","ref_id":6386}}
JSON.
org.json.JSONObject cannot be converted to JSONArray in Android Studio
Your json data is of type JSONObject. The JSONArray you want is nested in it under key "result"
. You can retrieve it like this
JSONArray ja = new JSONObject(jsonData).getJSONArray("result"));
type org.json.JSONObject cannot be converted to JSONArray Android
valError
is not a Array. It is an object. You should get it like
JsonObject jsonValError = jsonObject.getAsJsonObject("valError")
And for that you can get the field username
for example like
String username = jsonValError.getString("username")
at response of type org.json.JSONObject cannot be converted to JSONArray
your php file produced a json object, not a json array.
so, try JSONObject arr =object.getJSONObject("response");
By the way, your logcat had already reminded you to treat it as a JSONObject.
org.json.JSONException cannot be converted to JSONArray in android
A JSONArray string is enclosed in []
, while a JSONObject string is enclosed in {}
. The string you are trying to parse is not a JSONArray - it is a JSONObject, so instead of
JSONArray jsonarray = new JSONArray(response);
you need to use
JSONObject jsonObj = new JSONObject(response);
That's why the error message says type org.json.JSONObject cannot be converted to JSONArray
.
Once you have the object, you can get objects or arrays that are nested within in. In this case, your JSON string looks like this:
{"dsdata":{"ttlogin":[{"BundyId":"9090","Name":"IT DEVELOPMENT"}]}}
so if you wanted to get the "ttlogin" array you could use
JSONObject jsonObj = new JSONObject(response);
JSONArray arr = jsonObj.getJSONObject("dsdata").getJSONArray("ttlogin");
Error JSONObject cannot be converted to JSONArray in android?
And I final got it to work this is my current code
private void insertToDb() throws JSONException{
//createJsonArray();
final String jsonArray = itemSelectedJson.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, INVEST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
display.setText("This is the "+response);
Toast.makeText(AddInvEst.this, "This is the response"+response, Toast.LENGTH_LONG).show();
Log.d("RESPONSE", response.toString().trim());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put(KEY_JSONARRAY,jsonArray);
return params;
}
};
RequestQueue requestQ =Volley.newRequestQueue(this);
requestQ.add(stringRequest);
}
Related Topics
Why Was "Avoid Enums Where You Only Need Ints" Removed from Android's Performance Tips
How to Display Input Errors in Popup
How to Create Custom UI for Android Mediacontroller
Navigation Drawer Semi-Transparent Over Status Bar Not Working
Android Persistence Room: "Cannot Figure Out How to Read This Field from a Cursor"
Android Oncreate or Onstartcommand for Starting Service
Livedata Update on Object Field Change
Android: Automatically Choose Debug/Release Maps API Key
Getapplicationcontext(), Getbasecontext(), Getapplication(), Getparent()
A Failure Occurred While Executing Com.Android.Build.Gradle.Internal.Tasks
Start an Activity from a Fragment
Disconnect a Bluetooth Socket in Android
Navigation Drawer: Set as Always Opened on Tablets
Difference Between APK (.Apk) and App Bundle (.Aab)
Memory Leaks with Custom Font for Set Custom Font
Toggle Button Using Two Image on Different State