1

i have a problem with my code and other post cannot helped me that's why i post here. I'm getting some json from my php like

{
    "lesMots":{
        "1":{
            "id":"4",
            "wordFrench":"bite",
            "wordEnglish":"dick"
        },
        "2":{
            "id":"7",
            "wordFrench":"pute",
            "wordEnglish":"whore"
        },
        "3":{
            "id":"2",
            "wordFrench":"ordinateur",
            "wordEnglish":"computer"
        },
        "4":{
            "id":"1",
            "wordFrench":"bonjour",
            "wordEnglish":"hello"
        },
        "5":{
            "id":"3",
            "wordFrench":"monde",
            "wordEnglish":"world"
        }
    }
}

Here is my Volley Request, i know i must use JSONObject for my JSONArray, but when i change it, the second JSONObject word ask me string and not int ! i'm trying to get 5 random french word and english word

send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/pendu_android.php",
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            try {
                                JSONArray jsonArray = response.getJSONArray("lesMots");
                                for (int i = 0; i < jsonArray.length();i++){
                                    JSONObject word = jsonArray.getJSONObject(i);

                                    String wordFrench = word.getString("wordFrench");
                                    String wordEnglish = word.getString("wordEnglish");
                                    textView.append(wordFrench+" "+wordEnglish+" "+" \n ");
                                    Toast.makeText(getApplicationContext(), wordFrench +"",Toast.LENGTH_LONG).show();
                                }
                            }catch (JSONException e){
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(), e +"",Toast.LENGTH_LONG).show();
                            }


                        }
                    },

                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "ERROR");
                            Toast.makeText(getApplicationContext(), error +"",Toast.LENGTH_LONG).show();

                        }
                    }


            );
            queue.add(jsonObjectRequest);
        }
    });

Thank for your help, i'm blocked on this problem and i'm kind of new in this. If you need something more tell me

3
  • 1
    lesMots is not JsonArray, its JsonObject, There is no JsonArray in your response. Please check difference between JsonObject and JsonArray. Commented Apr 4, 2016 at 12:52
  • you are trying to access the object using array, lesMots is object not an array. Commented Apr 4, 2016 at 12:54
  • Yeah i know that but i i don't know how to change that because when i do that by try { JSONObject jsonArray= response.getJSONObject("lesMots"); for (int i = 0; i < jsonArray.length();i++){ JSONObject word = jsonArray.getJSONObject(i); he ask me an string on the i but i don't know what to put here Commented Apr 4, 2016 at 12:56

3 Answers 3

2

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Try again, jObject should be lesMots. I changed the example code.
Yes, i tried that too but i have a Incompatible type here JSONObject obj = lesMots.get(key);
cast the object (JSONObject) lesMots.get(key);
You can use obj to get the translations, obj.getString("wordFrench");
Oh, okay that's working now ! i'm really sorry, don't know what i didn't do that :) that's works now thanks !
0

You have to change jsonarray to jsonobject.

send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/pendu_android.php",
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                try {
                                    JSONObject jsonArray = response.getJSONObject("lesMots");
                                    for (int i = 0; i < jsonArray.length();i++){
                                        JSONObject word = jsonArray.getJSONObject(i);

                                        String wordFrench = word.getString("wordFrench");
                                        String wordEnglish = word.getString("wordEnglish");
                                        textView.append(wordFrench+" "+wordEnglish+" "+" \n ");
                                        Toast.makeText(getApplicationContext(), wordFrench +"",Toast.LENGTH_LONG).show();
                                    }
                                }catch (JSONException e){
                                    e.printStackTrace();
                                    Toast.makeText(getApplicationContext(), e +"",Toast.LENGTH_LONG).show();
                                }


                            }
                        },

                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("Volley", "ERROR");
                                Toast.makeText(getApplicationContext(), error +"",Toast.LENGTH_LONG).show();

                            }
                        }


                );
                queue.add(jsonObjectRequest);
            }
        });

1 Comment

I already try that but he ask me a String instead of i here JSONObject word = jsonArray.getJSONObject(i);
-1

the issue is that you have an obaject not an array solution can be this: (where you response is the same you receive from the callback)

for example to mock this you can do:

   String responseJson = "{\"lesMots\":{\"1\":{\"id\":\"4\",\"wordFrench\":\"bite\"," +
                    "\"wordEnglish\":\"dick\"},\"2\":{\"id\":\"7\",\"wordFrench\":\"pute\"," +
                    "\"wordEnglish\":\"whore\"},\"3\":{\"id\":\"2\"," +
                    "\"wordFrench\":\"ordinateur\",\"wordEnglish\":\"computer\"}," +
                    "\"4\":{\"id\":\"1\",\"wordFrench\":\"bonjour\",\"wordEnglish\":\"hello\"}," +
                    "\"5\":{\"id\":\"3\",\"wordFrench\":\"monde\",\"wordEnglish\":\"world\"}}}";
JSONObject response = new JSONObject(responseJson);

then in your callback

@Override
                        public void onResponse(JSONObject response) {
     try {
            JSONObject lesMotsObject = response.getJSONObject("lesMots");
            JSONArray jsonArray = lesMotsObject.toJSONArray(lesMotsObject.names());

                for (int i = 0; i < jsonArray.length();i++){
                    JSONObject word = jsonArray.getJSONObject(i);

                    String wordFrench = word.getString("wordFrench");
                    String wordEnglish = word.getString("wordEnglish");
...

5 Comments

your response object it he initial object
Oh yeah that's work ! this is so complicated for a newbie like me :O thanks a lot !
From performance point of view this isn't a good solution. Since it is already parsed you are able to iterate through the keys that are in the JSONObject.
It will be enough, it's juste a little app and i'm not doing a lot of request so that's okay thanks !
@Exaqt is right and for big json responses this not optimal as it does an extra iteration over the elements. in that case his solution can be used of just the web service api can be optimized or use github.com/apptik/JustJson where you can iterate straight on the values :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.