0

 json是一个带有关键“消息”的 NSMutableDictionary。我需要使用“清理”该密钥中的数据stringByReplacingOccurrencesOfString

NSLog(@"json: %@", json);输出这个:

json: {
    conversationId = 61;
    countmessagesinconversation = 2;
    message = "Messages exist!";
    messagesinconversation =     (
                {
            message = "Hi";
            messagecreated = "June 24, 2013 16:16";
            messageid = 68;
            sentby = Thomas;
            sentbyID = 1;
            title = "Subject";
        },

                {
            message = "What's  up?";
            messagecreated = "September 22, 2013 17:00";
            messageid = 331;
            sentby = Steve;
            sentbyID = 2;
            title = "Subject";
        }
    );
    success = 1;
}

所以,这就是我想出的,但我显然弄错了。

NSString *newstr = [[NSString alloc]init];

    for (newstr in [[[json objectForKey:@"messagesinconversation"]allKeys]copy]){
        newstr = [newstr stringByReplacingOccurrencesOfString:@" "
                                             withString:@""];
        [json setValue:newstr forKey:@"message"];
    }

有人可以帮我解决这个问题并解释一下,以便我理解这个概念吗?我知道如何使用数组执行此操作,但我的问题是(我认为)我不完全了解如何访问字典中的正确键。

编辑:对不起,我的问题不清楚。发生的情况是,如果键中有两个空格字符,message那么" "当我在屏幕上显示它时就会显示出来。

4

3 回答 3

1
//First get the array of message dictionaries:
NSArray * messages = [json objectForKey:@"messagesinconversation"];

//create new array for new messages
NSMutableArray * newMessages = [NSMutableArray arrayWithCapacity:messages.count];

//then iterate over all messages (they seem to be dictionaries)
for (NSDictionary * dict in messages)
{
    //create new mutable dictionary
    NSMutableDictionary * replacementDict = [NSMutableDictionary dictionaryWithDictionary:dict];

    //get the original text
    NSString * msg = [dict objectForKey:@"message"];

    //replace it as you see fit
    [replacementDict setObject:[msg stringByReplacingOccurrencesOfString:@" " withString:@""] forKey:@"message"];

    //store the new dict in new array
    [newMessages addObject:replacementDict];
}

//you are done - replace the messages in the original json dict
[json setObject:newMessages forKey:@"messagesinconversation"];
于 2013-09-23T08:54:20.110 回答
0

将您的字典数组添加到另一个可变数组中

NSMutableArray *msgArray = [[json objectForKey:@"messagesinconversation"] mutableCopy];

用于for loop访问它。

for (int i = 0 ; i < msgArray.count ; i++)
{
     [[[msgArray objectAtindex:i] objectForKey:@"message"]  stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
}

[self.mainJSONDic setValue:msgArray forKey:@"messagesinconversation"];

试试这个代码可能对你的情况有帮助:

于 2013-09-23T08:01:25.210 回答
0

现在要求更清楚了,我将尝试一个答案。我尝试使用与您的问题相同的名称,并在错误的答案中提出建议。

在你声明它的地方做json一个。NSMUtableDictionary然后往前走:

json = [json mutableCopy]; // creates a mutable dictionary based on json which is immutable as result of the json serialization although declared as mutable. 

NSMutableArray *msgArray = [[json objectForKey:@"messagesinconversation"] mutableCopy];  //this fetches the array from the dictinary and creates a mutable copy of it. 
[json setValue:newstr forKey:@"messagesinconversation"];  // replace the original immutable with the mutable copy. 

for (int i = 0; i < [msgArray count]; i++) {
    NSMutableDictionary mutableInnerDict = [[msgArray objectAtIndex:i] mutableCopy]; // fetching the i-th element and replace it by a mutable copy of the dictionary within. 
    [msgArray replaceObjectAtIndex:i withObject:mutableInnerDict]; // it is now mutable and replaced within the array. 
    NSString *newString = [[[msgArray objectAtindex:i] objectForKey:@"message"]  stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];  // crates a new string with all &nbsp removed with blanks. 
    [mutableInnerDict setValue:newString forKey:@"message"];
}

关于用“”替换 ,这真的是你想要的吗?我问是因为   不会出现在您的示例数据中。或者你想完全删除空白?

于 2013-09-23T07:57:08.727 回答