org.json.JSONObject无法在我的android项目

时间:2016-04-04 12:51:23

标签: android

我的代码有问题而其他帖子无法帮助我,这就是我在这里发帖的原因。 我从我的php中获得了一些json,如

{
    "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"
        }
    }
}

这是我的Volley Request,我知道我必须为我的JSONArray使用JSONObject,但是当我更改它时,第二个JSONObject字会问我字符串而不是int! 我试图获得5个随机的法语单词和英语单词

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);
        }
    });

感谢您的帮助,我已经阻止了这个问题,我在这方面有点新意。 如果你需要更多的东西告诉我

3 个答案:

答案 0 :(得分:2)

试试这个,它会根据键遍历对象:

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();
}

答案 1 :(得分:0)

您必须将jsonarray更改为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);
            }
        });

答案 2 :(得分:-1)

问题是你有一个obaject而不是数组解决方案可以是这样的: (您的回复与回调中的回复相同)

例如嘲笑你可以这样做:

   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);

然后在你的回调中

@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");
...