我需要从JSON中删除双引号以用于NSDictionary

时间:2010-10-12 15:52:08

标签: php iphone json nsarray nsdictionary

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

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

这是代码         // rowsArray的JSON NSURL REQUEST

    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将无法保存

谢谢,

迈克尔

1 个答案:

答案 0 :(得分:0)

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

-[NSString stringByReplacingOccurrencesOfString: withString:]

像这样:

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

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