簡體   English   中英

我需要從JSON中刪除雙引號以在NSDictionary中使用

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

我有一個要保存到NSDictionary中的JSON返回值,但是,由於返回的數據中有空格,由於包含了引號,因此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不會保存

謝謝,

麥可

刪除所有引號的最簡單方法是使用:

-[NSString stringByReplacingOccurrencesOfString: withString:]

像這樣:

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

“ /”是引號的轉義字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM