2013-03-01 122 views
0

您好我有一个JSONObject,我已变成一个Array环通过该数组为一个变量,看看它是否存在,如果不创建一个我需要然后写这个回到阵列,然后回到JSON Object继承人帽子我到目前为止,但不能完全弄清楚如何将值写回到JSON Object更改JSON对象中Name()的值Android

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

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

       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; 

继承人什么被注销。我无法显示完整的数据,因为它的敏感性,并已被指示不发布。但什么即时试图做的是clubID的谷写入到一个JSONArray一个新的键值对,然后返回一个JSONObject

03-01 12:00:51.269: V/globals(1085): ARRAY userClubsDictKeys = ["6496","16885"] 
03-01 12:00:51.269: V/globals(1085): clubID = 6496 


      } 
+0

你可以发表你的JSON结果? – 2013-03-01 12:19:27

+0

@Tamil编辑我的问题与日志 – 2013-03-01 12:25:39

+0

试试这个:JSONArray userClubsDictKeys = new JSONArray(“user_clubs”);并打印此检查它是否获得价值 – 2013-03-01 12:28:23

回答

2

如果我理解正确的话,你要添加值JSONArray/JSONObject的。

要将值添加到JSONArray使用

JSONArray json = new JSONArray(); 
json.put(<value>); 

- 或 -

json.put(<index>, <value>); 

添加值一个JSONObject使用

JSONObject json = new JSONObject(); 
json.put("<key>", <value>);