简体   繁体   中英

Reading JSON file from dropbox iOS

I have a scenario where app reads file from server (dropbox) and checks version. If new version is available then download the app.

I'm trying to read file from link but getting null after JSON parsing.

NSError *error;
NSString *strFileContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://www.dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=0"] 
                                                    encoding:NSUTF8StringEncoding 
                                                       error:&error];

if(!error) {  //Handle error
    // NSLog(@"strFileContent: %@",strFileContent);
    NSData *data = [strFileContent dataUsingEncoding:NSUTF8StringEncoding];

    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"json : %@",[json description]);
}
else
    NSLog(@"Error in reading!");
}

Any alternative to DropBox?

There is a way, I find it quite ugly, but still.
First issue, as mentioned by other: yourURLEtcdl=1 .
Second big issue, the file is a rtf file, not a JSON object.
So, since there is a way going with RTF => NSAttributedString => NSString => "JSON" => Get your wanted value, here is what you could do:

NSError *error;
NSURL *url = [NSURL URLWithString:@"https://www.dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=1"];
NSString *strFileContent = [NSString stringWithContentsOfURL:url
                                                    encoding:NSUTF8StringEncoding
                                                       error:&error];

NSLog(@"strFileContent: %@", strFileContent);

if(!error)
{
    NSError *rtfError;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[strFileContent dataUsingEncoding:NSUTF8StringEncoding]
                                                                               options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil
                                                                                 error:&rtfError];
    if (rtfError)
    {
        NSLog(@"RTFError: %@", [rtfError localizedDescription]);
    }
    else
    {
        NSLog(@"string: %@", [attributedString string]);

        NSData *data = [[attributedString string] dataUsingEncoding:NSUTF8StringEncoding];

        NSError *jsonError;
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
        if (jsonError)
        {
            NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
        }
        else
        {
            NSLog(@"JSON: %@", jsonDict);
            NSInteger version = [[jsonDict objectForKey:@"Version"] integerValue];
            NSLog(@"Version: %ld", (long)version);
        }
    }
}
else
{
    NSLog(@"Error: %@", error);
}

As I said, I don't find these very great, it works, but this solution seems to me quite messy (passing twice with NSData ).

That's not a text file containing JSON; it's an RTF file containing JSON. That won't work with NSJSONSerialization :

{\rtf1\ansi\ansicpg1252\cocoartf1347\cocoasubrtf570
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 \{\
  "Version": 1.0,\
  "ImageList": [\
    "Nature.png",\
    "Nature-1.png",\
    "Ballon.png"\
  ]\
\}}

You've saved that file using an editor that uses RTF (the default Mac TextEdit ). you'll need to re-save as plain text.

Provided URL is incorrect too. If you need only file contents you have to set dl=1 (last component of url). Like so -

dropbox.com/s/22mm417fxdqdn8c/FileStructure.txt?dl=1

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