Rodrigo Costa
Rodrigo Costa

Reputation: 39

ERROR converting json

I need help converting json.

Here is my code:

try {
    JSONObject mainJson22 = new JSONObject(reply);

    JSONArray jsonArray22 = mainJson22.getJSONArray("UserListByBusinessAreaContextResult");
    Log.i("mainjson234","" +  jsonArray22);

    for (int i2 = 0; i2 < jsonArray22.length(); i2++) {
        JSONObject objJson22 = jsonArray22.getJSONObject(i2);   
//      JSONArray innerJsonArray = jsonArray.getJSONArray(i);
//      JSONObject objJson = innerJsonArray.getJSONObject(0);

        int id22 = objJson22.getInt("UserID");
        String username22 =objJson22.getString("Username"); 
        String name = objJson22.getString("Name");

        reply =  {"UserListByBusinessAreaContextResult":"{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"}

and the log:

org.json.JSONException: Value {"tableData":[{"UserID":30,"Username":"Teste","Name":"Teste"}]} at UserListByBusinessAreaContextResult of type java.lang.String cannot be converted to JSONArray

The log says that the error is in this line :

mainJson22.getJSONArray("UserListByBusinessAreaContextResult");

Upvotes: 0

Views: 70

Answers (1)

CtrlX
CtrlX

Reputation: 7015

UserListByBusinessAreaContextResult

is not an Array. It's a JSONObject.

tableData is your JSONArray. So you must have :

try { 
JSONObject mainJson22 = new JSONObject(reply);
JSONArray jsonResult = mainJson22.getJSONObject("UserListByBusinessAreaContextResult");
JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");
Log.i("mainjson234","" +  jsonArray22);

for (int i2 = 0; i2 < jsonArray22.length(); i2++) {

   JSONObject objJson22 = jsonArray22.getJSONObject(i2);
...

Upvotes: 1

Related Questions