我需要从 JSON 中删除双引号以便在 NSDictionary 中使用

发布于 2024-09-27 03:22:12 字数 1469 浏览 1 评论 0原文

我有一个 JSON 返回,我试图将其保存到 NSDictionary 中,但是,由于返回的数据中有空格,字典不会保存此数组,因为包含引号。有没有办法在保存到 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];

这很好用,如果雪佛兰所在的字符串可以保存,但如果双引号在那里,.plist 将不会保存

谢谢,

迈克尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-10-04 03:22:12

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

-[NSString stringByReplacingOccurrencesOfString: withString:]

如下所示:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文