JSONObject无法转换为JSONArray错误

时间:2012-10-04 06:23:27

标签: android android-intent

我有这样的代码来显示来自mysql的数据,

用于读取数据

protected String doInBackground(String... params) {
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                       // Building Parameters
                //  int success;
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("phone", mPhoneNumber));
                        params.add(new BasicNameValuePair("id_store", id_store));

                        // getting store details by making HTTP request
                        // Note that store details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(Constants.url_store_detail, "GET", params);

                        // check your log for json response
                        Log.d("Single Store Details", json.toString());

                        // json success tag

                            // successfully received store details
                            JSONArray storeObj = json.getJSONArray("store"); // JSON Array

                            // get first product object from JSON Array
                            JSONObject store = storeObj.getJSONObject(0);

                            // product with this pid found
                            // Edit Text
                            urledit = (EditText) findViewById(R.id.ETurledit);
                            storenameedit = (EditText) findViewById(R.id.ETusernameedit);
                            storedescedit = (EditText) findViewById(R.id.ETdescedit);
                            edittype=(Spinner)findViewById(R.id.Stypeedit);

                            String url = json.getString(TAG_URL);

                            // display store data in EditText
                            urledit.setText(store.getString(TAG_URL));
                            storenameedit.setText(store.getString(TAG_NAME));
                            storedescedit.setText(store.getString(TAG_DESC));               

                            if(store.getString(TAG_TYPE).equals("1")){
                                String mine = store.getString("Wordpress");
                                ArrayAdapter myadap = (ArrayAdapter)edittype.getAdapter();

                                int spinnerpos = myadap.getPosition(mine);
                                edittype.setSelection(spinnerpos);

                            }else{
                                String mine = store.getString("Prestashop");
                                ArrayAdapter myadap = (ArrayAdapter)edittype.getAdapter();

                                int spinnerpos = myadap.getPosition(mine);

                                edittype.setSelection(spinnerpos);


                            }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

但它显示错误

10-04 13:08:57.377: DEBUG/dalvikvm(306): GC_EXPLICIT freed 42 objects / 2008 bytes in 260ms
10-04 13:08:59.837: INFO/ActivityManager(59): Starting activity: Intent { cmp=shoop3.android.edu/.editstoreActivity (has extras) }
10-04 13:09:01.148: DEBUG/dalvikvm(1272): GC_FOR_MALLOC freed 1456 objects / 269496 bytes in 198ms
10-04 13:09:01.407: DEBUG/Single Store Details(1272): {"store":{"id_user":"56","id_platform":"2","store_url":"prestashop","store_desc":"dasdasd","id_store":"38","store_logo":null,"store_name":"prestashop","store_curr":null}}
10-04 13:09:01.427: WARN/System.err(1272): org.json.JSONException: Value {"id_user":"56","id_platform":"2","store_url":"prestashop","store_desc":"dasdasd","id_store":"38","store_logo":null,"store_name":"prestashop","store_curr":null} at store of type org.json.JSONObject cannot be converted to JSONArray
10-04 13:09:01.457: WARN/System.err(1272):     at org.json.JSON.typeMismatch(JSON.java:96)
10-04 13:09:01.457: WARN/System.err(1272):     at org.json.JSONObject.getJSONArray(JSONObject.java:548)
10-04 13:09:01.507: WARN/System.err(1272):     at shoop3.android.edu.editstoreActivity$GetStoreDetails$1.run(editstoreActivity.java:189)
10-04 13:09:01.507: WARN/System.err(1272):     at android.os.Handler.handleCallback(Handler.java:587)
10-04 13:09:01.517: WARN/System.err(1272):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 13:09:01.517: WARN/System.err(1272):     at android.os.Looper.loop(Looper.java:123)
10-04 13:09:01.517: WARN/System.err(1272):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-04 13:09:01.528: WARN/System.err(1272):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 13:09:01.528: WARN/System.err(1272):     at java.lang.reflect.Method.invoke(Method.java:521)
10-04 13:09:01.528: WARN/System.err(1272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-04 13:09:01.537: WARN/System.err(1272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-04 13:09:01.577: WARN/System.err(1272):     at dalvik.system.NativeStart.main(Native Method)
10-04 13:09:02.948: INFO/ActivityManager(59): Displayed activity shoop3.android.edu/.editstoreActivity: 3002 ms (total 3002 ms)

如何解决这个问题?因为它显示JSONObject无法转换为JSONArray的错误, 我希望有人可以帮助我,谢谢你的建议

2 个答案:

答案 0 :(得分:1)

通过查看您的日志,请使用此

JSONObject storeObj = json.getJSONObject("store");

而不是

JSONArray storeObj = json.getJSONArray("store");

因为store是一个JSON对象

答案 1 :(得分:0)

JSONObject json = jsonParser.makeHttpRequest(Constants.url_store_detail, "GET", params);

你可以看到这里的json是JSONObject。在这里

JSONArray storeObj = json.getJSONArray("store");
来自JSONObject

您正试图获得JSONArray

所以不要将json声明为JSONObject,而是将其声明为JSONArray

然后删除storeObj并直接获取您的数据,如下所示

JSONObject store = json.getJSONObject(0);