2013-03-01 72 views
-2

嗨即时获取JSON异常错误,但不知道为什么任何人都可以帮忙?无法将字符串转换为JSONObject错误Android

继承人我的代码

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) 
+0

什么是你的输入/输出? – 2013-03-01 16:14:08

+0

这是什么回报:GlobalActions.returnDataFromFile(null,“mynetwork.json”); ? – 2013-03-01 16:14:26

+0

它返回一个JSONObject – 2013-03-01 16:15:01

回答

-2

解决它的尝试和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"); 


      } 
     } 
相关问题