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

时间:2022-09-15 13:02:54

I have a JSON return that I'm trying to save into a NSDictionary, However, Since there are spaces in the data being returned, the Dictionary won't save this array because of the quote being included. Is there a way to parse the SBJSON to remove the double quotes prior to saving to the rowsArray?

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

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

Here is the code //JSON NSURL REQUEST for rowsArray

这是代码// 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];

This works Great and saves if the string where Chevy is, but if the double quotes are in there the .plist won't save

如果Chevy所在的字符串,这可以很好地保存,但如果双引号在那里,.plist将无法保存

Thanks,

谢谢,

Michael

迈克尔

1 个解决方案

#1


0  

The easiest way to remove all the quotes would be to use:

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

-[NSString stringByReplacingOccurrencesOfString: withString:]

- [NSString stringByReplacingOccurrencesOfString:withString:]

like so:

像这样:

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

the "/" is the escape character for the quotes.

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

#1


0  

The easiest way to remove all the quotes would be to use:

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

-[NSString stringByReplacingOccurrencesOfString: withString:]

- [NSString stringByReplacingOccurrencesOfString:withString:]

like so:

像这样:

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

the "/" is the escape character for the quotes.

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