-2

嗨,我收到 JSON 异常错误,但不知道为什么有人可以帮忙?

这是我的代码

public static void updateLocalDataWithObject(Context activityContext, JSONObject fullNetworkDataFromFile) throws JSONException{

             JSONObject userPrefsDict = new JSONObject();
             JSONObject fullNetworkfile = fullNetworkDataFromFile.getJSONObject("data");
             JSONObject tempUserDict = fullNetworkfile.getJSONObject("user");
             String user_pref_club = tempUserDict.getString("primary_club_id");
             Log.v("globals", "3.)user_pref_club = " + user_pref_club);
             JSONObject userClubsDict = fullNetworkfile.getJSONObject("user_clubs");
             Log.v("globals", "userClubsDict = " + userClubsDict);

             if(userClubsDict.length()!= 0){
                 try { 


                 JSONArray userClubsDictKeys = new JSONArray();          
                 userClubsDictKeys=userClubsDict.names();

                 ArrayList<String> stringArray = new ArrayList<String>();
                 for(int i = 0, count = userClubsDictKeys.length(); i< count; i++)
                 {

                         JSONObject jsonObject = userClubsDictKeys.getJSONObject(i);
                         stringArray.add(jsonObject.toString());

                 }

                 for(String myString : stringArray){
                     Log.v("globals", "myString = " + myString);

                 }

                 Log.v("globals", "ARRAY userClubsDictKeys = " + userClubsDictKeys);
                 String clubID = userClubsDictKeys.getString(0);
                 Log.v("globals", "clubID = " + clubID);

                 if(user_pref_club =="" || (userClubsDictKeys.getString(0) == null)) user_pref_club = clubID; 

                 userClubsDict.put("selected_club", user_pref_club);
                 Log.v("globals", "userClubsDict EDITED = " + userClubsDict);

                 fullNetworkfile.put("user_prefs", userClubsDict);
                 Log.v("globals", "fullNetworkfile EDITED = " + fullNetworkfile);


                 String writableString  = fullNetworkfile.toString();
                 writeDataToFile(activityContext,writableString, "mynetwork.json");
                 Log.v("globals", "fullNetworkfile Written = to FIle");
                 JSONObject returnDataFromFile = GlobalActions.returnDataFromFile(null,"mynetwork.json");
                 Log.v("globals", "returnDataFromFile= " + returnDataFromFile);
                 JSONObject userprefsdata = returnDataFromFile.getJSONObject("user_prefs");
                 Log.v("globals", "user_prefs= " + userprefsdata);

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

这是错误

03-01 15:34:11.219: W/System.err(2462): org.json.JSONException: Value 6496 at 0 of type java.lang.String cannot be converted to JSONObject
03-01 15:34:11.229: W/System.err(2462):     at org.json.JSON.typeMismatch(JSON.java:96)
03-01 15:34:11.229: W/System.err(2462):     at org.json.JSONArray.getJSONObject(JSONArray.java:484)
03-01 15:34:11.229: W/System.err(2462):     at com.myApp.myApp.GlobalActions.updateLocalDataWithObject(GlobalActions.java:346)
03-01 15:34:11.229: W/System.err(2462):     at com.myApp.myApp.LoginForm.apiNetworkCallResponse(LoginForm.java:268)
03-01 15:34:11.229: W/System.err(2462):     at com.myApp.myApp.LoginForm$loginSuccessTask.onPostExecute(LoginForm.java:255)
03-01 15:34:11.229: W/System.err(2462):     at com.myApp.myApp.LoginForm$loginSuccessTask.onPostExecute(LoginForm.java:1)
03-01 15:34:11.229: W/System.err(2462):     at android.os.AsyncTask.finish(AsyncTask.java:417)
03-01 15:34:11.229: W/System.err(2462):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-01 15:34:11.229: W/System.err(2462):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-01 15:34:11.229: W/System.err(2462):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 15:34:11.229: W/System.err(2462):     at android.os.Looper.loop(Looper.java:123)
03-01 15:34:11.229: W/System.err(2462):     at android.app.ActivityThread.main(ActivityThread.java:3687)
03-01 15:34:11.229: W/System.err(2462):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 15:34:11.229: W/System.err(2462):     at java.lang.reflect.Method.invoke(Method.java:507)
03-01 15:34:11.229: W/System.err(2462):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-01 15:34:11.229: W/System.err(2462):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-01 15:34:11.229: W/System.err(2462):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

-2

解决了它在try and catch中移动我的JSON,它似乎有效

public static void updateLocalDataWithObject(Context activityContext, JSONObject fullNetworkDataFromFile) throws JSONException{


             JSONObject fullNetworkfile = fullNetworkDataFromFile.getJSONObject("data");
             JSONObject tempUserDict = fullNetworkfile.getJSONObject("user");
             String user_pref_club = tempUserDict.getString("primary_club_id");
             Log.v("globals", "3.)user_pref_club = " + user_pref_club);
             JSONObject userClubsDict = fullNetworkfile.getJSONObject("user_clubs");
             Log.v("globals", "userClubsDict = " + userClubsDict);

             if(userClubsDict.length()!= 0){

                 JSONObject userPrefsDict = new JSONObject();

                 JSONArray userClubsDictKeys = new JSONArray();          
                 userClubsDictKeys=userClubsDict.names();

                 ArrayList<String> stringArray = new ArrayList<String>();
                 for(int i = 0, count = userClubsDictKeys.length(); i< count; i++)
                 {
                     try {
                         JSONObject jsonObject = userClubsDictKeys.getJSONObject(i);
                         stringArray.add(jsonObject.toString());
                     }
                     catch (JSONException e) {
                         e.printStackTrace();
                     }
                 }

                 String clubID = userClubsDictKeys.getString(0);

                 if(user_pref_club =="" || (userClubsDictKeys.getString(0) == null)) user_pref_club = clubID; 

                 userPrefsDict.put("selected_club", user_pref_club);
                 fullNetworkfile.put("user_prefs", userPrefsDict);


                 String writableString  = fullNetworkfile.toString();
                 writeDataToFile(activityContext ,writableString, "mynetwork.json");


             } 
        }
于 2013-03-01T17:31:59.693 回答