嗨我有一个JSONObject
我已经变成一个Array
循环通过该数组的一个变量,看它是否存在,如果没有创建一个我需要然后把它写回数组然后回到我迄今为止所见的JSON Object
继承人的帽子,但是不能弄清楚如何将值写回JSON Object
?
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中的新keyvalue对,然后返回到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 :(得分:2)
如果我理解正确,您希望将值添加到JSONArray / JSONObject。
要将值添加到JSONArray,请使用
JSONArray json = new JSONArray();
json.put(<value>);
-OR -
json.put(<index>, <value>);
要将值添加到JSONObject,请使用
JSONObject json = new JSONObject();
json.put("<key>", <value>);