0

I have one JSON: myJSON
But vhen i try to Parse it, I have some problem like this:

07-29 18:51:21.203 4995-4995/? W/System.err﹕ org.json.JSONException: Value {"id":2,"max_volume":20,"current_volume":6,"name":"Подняться на 16 этаж за 5 минут пешком и соскользить вниз","description":"Кто провалит испытание, с криком 'Это СПАРТА!' будет скинут с 16 этажа. Кто провалит испытание, с криком 'Это СПАРТА!' будет скинут с 16 этажа. Кто провалит испытание, с криком 'Это СПАРТА!' будет скинут с 16 этажа. Кто провалит испытание, с криком 'Это СПАРТА!' будет скинут с 16 этажа АЗАЗА что-нибудь ещё что занимает много текста, ха-ха!","max_count":10,"date_from":"2015-05-05","date_to":"2015-05-15","create_time":"2015-05-04 23:00 GMT+03:00","status":"NOT_STARTED","update_date":"2015-05-05 08:00 GMT+03:00","category":"NON_STUDY","subject":{"subject_id":1,"type":"Foreign Languages","name":"English"},"users":[{"id":1,"first_name":"Irina","last_name":"Noise","img":"https://avatars3.githubusercontent.com/u/1412561?v=3&s=460"},{"id":13,"first_name":"Alex","last_name":"Gra","img":"https://avatars2.githubusercontent.com/u/964601?v=3&s=460"}],"badge":{"badge_id":1,"img":"https://www.gravatar.com/avatar/e514b017977ebf742a418cac697d8996?s=64&d=identicon&r=PG","name":"EnglishPro","subject":{"id":1,"name":"English","type":"Foreign Languages"},"practice":10,"theory":0,"date":"2015-05-07 14:30 GMT+03:00","progress":0.14}} of type org.json.JSONObject cannot be converted to JSONArray

I tried like this:
Service code:

public class getChallenge extends AsyncTask<Void, Void, String> {

        String mUrl;
        OkHttpClient client = new OkHttpClient();
        String result = "";

        public getChallenge(String url) {
            this.mUrl = url;
        }

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        @Override
        protected String doInBackground(Void... params) {
            Log.d("Background", "inBackground");
            try {
                URL mUrl = new URL(url);
                urlConnection = (HttpURLConnection) mUrl.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                result = buffer.toString();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String strJson) {
            super.onPostExecute(strJson);
            Log.d(LOG_TAG, strJson);
            Exercise exercise = new Exercise();
            exercise = ParseJSON.ExcerciseParseJSON(strJson);
//            Log.d("EXERSICE", exercise.getName());
        }
    }

and my Parse method:

public static Exercise ExcerciseParseJSON(String strJson) {
    Log.d("ExcerciseParseJSON", "working");
    Exercise exercise = new Exercise();
    try {
        JSONArray array = new JSONArray(strJson);
            exercise.setId(array.getJSONObject(0).getLong("id"));
            exercise.setMax_volume(array.getJSONObject(0).getInt("max_volume"));
            exercise.setCurrent_volume(array.getJSONObject(0).getInt("current_volume"));
            exercise.setName(array.getJSONObject(0).getString("name"));
            exercise.setDescription(array.getJSONObject(0).getString("description"));
            exercise.setMax_count(array.getJSONObject(0).getInt("max_count"));
            exercise.setDate_from(array.getJSONObject(0).getString("date_from"));
            exercise.setDate_to(array.getJSONObject(0).getString("date_to"));
            exercise.setCreate_time(array.getJSONObject(0).getString("create_time"));
            exercise.setStatus(array.getJSONObject(0).getString("status"));
            exercise.setUpdate_time(array.getJSONObject(0).getString("update_date"));
            exercise.setCategory(array.getJSONObject(0).getString("category"));
    }  catch (JSONException e) {
        e.printStackTrace();
    }
    return exercise;
}

Where is my mistake? Or you can give me right piece of code:)

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
metalink
  • 594
  • 2
  • 8
  • 21

3 Answers3

0

Just use JSONObject to store your json as an object after using JSONTokener()..

From it you can do what ever you do now.

JSONObject array = (JSONObject) new JSONTokener(strJson).nextValue();

And,

exercise.setId(array.getLong("id"));

Just in case, you want to fetch the users list

you can use an array like,

JSONArray users = array.getJSONArray("users")
Maheswaran Ravisankar
  • 17,652
  • 6
  • 47
  • 69
0

JSONObject array = new JSONObject(strJson);

use this and see

Lingeshwaran
  • 579
  • 4
  • 15
0

Try this

    JSONObject object = new JSONObject(strJson);

    exercise.setMax_volume(object.getInt("max_volume"));
    // ... and so one

    // Get array of users
    JSONArray usersArray = new JSONArray(object.getString("users"));

    // Get info for user 0
    exercise.setId(usersArray.getJSONObject(0).getLong("id"));
    // ... and so one
Fox
  • 417
  • 4
  • 13