简体   繁体   中英

I need to remove double quotes from JSON for use in NSDictionary

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?

  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

    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

Thanks,

Michael

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

-[NSString stringByReplacingOccurrencesOfString: withString:]

like so:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM