【问题标题】:Filter and replace all the occurence of string in ios过滤并替换ios中所有出现的字符串
【发布时间】:2014-04-08 13:53:47
【问题描述】:

我有以下格式的数据

   field1 = "sometext";
    field2 = text2;
    field3 =         (
                    {
            uniqueId = 123;
        },
                    {
            uniqueId = 234;
        }
    );
    field4 = "anothertext";

我需要这样转换

"field1":"sometext",
"field2":"text2",
"field3": [
                {
        "uniqueId":"123",
    },
                {
        "uniqueId":"234",
    }
],
"field4":"anothertext"

即用 : 替换 '=' 并在 '=' 之前和之后用 " 和 ; 替换字符串的结尾和结尾我应该如何进行?

【问题讨论】:

  • 您只是想将一个对象转换为 JSON 吗?
  • 您在上述格式中使用的对象类型是什么,它是字符串对象或字典对象,否则您有任何文件
  • 使用NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];,其中dictionary 是您在第一个示例中显示的对象。见NSJSONSerialization Class Reference
  • 该对象是一个字符串对象,实际上我有一个文本视图,我将在其中复制此日志,并尝试将其转换为 JSON 格式。

标签: ios objective-c replace nsstring


【解决方案1】:

您似乎有一个字典,其中包含一对字符串键/值对和一组您试图转换为 JSON 的其他字典。

如果我对此有误并完全错过了您要完成的工作,我深表歉意。根据您想要的最终结果,这可能是一种更简单的方法。

你当前的初始字符串可以这样存储:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                         @"sometext",   @"field1",
                                         text2,         @"field2",
                                         myDictArray,   @"field3",
                                         @"anothertxt", @"field4"
                      , nil];

然后使用json转换转换成你想要的格式。

我有一个方法可以用来做这件事。

+(NSData*)jsonDatafromDictionary:(NSMutableDictionary*) dictionary
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                   options:0 // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

    if (error) {
        NSString *errorDesc = [NSString stringWithFormat:@"Error creating json data from dictionary: %@", error.localizedDescription];
        NSLog(@"ERROR: %@", errorDesc);
        jsonData = nil;
        return nil;
    }
    else
        return jsonData;
}

要以 NSString 形式查看数据,您可以使用以下命令:

NSString *jsonString = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];

要将 json 转换回字典格式,我使用以下方法:

+(NSMutableDictionary*)dictionaryWithContentsOfJSONString:(NSString*)jsonString
{
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    if (error)
        NSLog(@"FAILED TO CREATE DICTIONARY FROM JSON STRING: %@", error.localizedDescription);
    jsonData = nil;
    error = nil;
    return jsonDict;
}

----- 编辑 ----- 根据您在下面的评论,这可能会让您入门。我唯一没有添加的是在需要的地方添加引号的代码……我需要考虑几分钟,这应该会替换所有其他字符,就像你想要的那样。

注意:我尚未对此进行测试,但应该可以帮助您入门。

-(NSString*)convertString:(NSString*)ps
{
    NSString *cs = @"";
    cs = [ps stringByReplacingOccurrencesOfString:@";" withString:@","];
    cs = [cs stringByReplacingOccurrencesOfString:@"(" withString:@"["];
    cs = [cs stringByReplacingOccurrencesOfString:@")" withString:@"]"];
    cs = [cs stringByReplacingOccurrencesOfString:@"=" withString:@":"];
    return cs;
}

【讨论】:

  • 感谢您的回复,实际上我有一个文本视图,我将在其中复制此日志,并尝试将其转换为 JSON 格式。目前我将 textview 值存储在 NSString 中,是否可以使用 NSJSONSerialization 来隐藏它?
  • 更新了我的答案,检查底部的编辑。我需要考虑报价,我的大脑被油炸了。如果您没有提出解决方案,我会回来查看并在我想清楚一点时尝试完成它。
猜你喜欢
  • 2014-08-05
  • 2019-01-10
  • 2018-07-11
  • 2012-08-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
相关资源
最近更新 更多