【问题标题】:I need to remove double quotes from JSON for use in NSDictionary我需要从 JSON 中删除双引号以在 NSDictionary 中使用
【发布时间】:2010-10-12 15:52:08
【问题描述】:

我有一个 JSON 返回,我试图将它保存到 NSDictionary 中,但是,由于返回的数据中有空格,因此 Dictionary 不会保存这个数组,因为包含了引号。有没有办法在保存到 rowsArray 之前解析 SBJSON 以删除双引号?

  rowsArray: {
 Rows =     (
            {
        Children =             (
                            {
                Title = Chevy;
            },
                            {
                Title = "Red Color";
            },
                            {
                Title = "Pre - 1965";
            },
                            {
                Title = "White Walls";
            },           
        );
        Title = Chevy;
    },

这里是代码 //JSON NSURL REQUEST for rowsArray

    NSURL *url = [NSURL URLWithString:@"http://www.**.php"];
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url ]


    SBJSON *json = [[SBJSON alloc] init];
    NSError *error = nil;
    rowsArray= [json objectWithString:jsonreturn error:&error];

NSLog(@"rowsArray: %@",rowsArray);


//SAVING rowsArray as "FollowingArray.plist"

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path2 = [documentsDirectory stringByAppendingPathComponent:@"FollowingArray.plist"];


    [rowsArray writeToFile:path2 atomically:NO];


    [jsonreturn release];
    [json release];

这很好用,如果 Chevy 所在的字符串可以保存,但如果双引号在其中,.plist 将不会保存

谢谢,

迈克尔

【问题讨论】:

    标签: php iphone json nsarray nsdictionary


    【解决方案1】:

    删除所有引号的最简单方法是使用:

    -[NSString stringByReplacingOccurrencesOfString: withString:]

    像这样:

    NSString *cleanedString=[jsonreturn stringByReplacingOccurrencesOfString:@"/"" withString:@""];
    

    “/”是引号的转义字符。

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2015-07-15
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多