Reading JSON file from dropbox iOS - 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

Related

How to get value from NSArray ios

I have got json response like following
Photo = ["\image1.jpg\","\image2.jpg\","\image3.jpg\"]
How can i get only name from this json response.
I want output like
Image1.jpg
Image2.jpg
Image3.jpg
Without [] and "".
First of all, Your Photo String is not in proper format. The correct String is as follows:
NSString *jsonString = #"[\"\image1.jpg\",\"\image2.jpg\",\"\image3.jpg\"]";
Try out the below code to get image name from Your String:
NSString *jsonString = #"[\"\image1.jpg\",\"\image2.jpg\",\"\image3.jpg\"]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
if (!json) {
NSLog(#"Error parsing JSON: %#", e);
} else {
NSLog(#"Item: %#", json);
for(NSString *item in json) {
NSLog(#"Item: %#", item);
}
}

Read data from geoJson file in ios

I'm trying to read data from geojson file using this code:
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:#"roads_json" ofType:#"geojson"];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[[NSData alloc]
initWithContentsOfFile:jsonPath]
options:0
error:nil];
NSLog(#"points:%#",json);
and the output:
points:(null)
any idea plz...
It is always best to pass an NSError object when using NSJSONSerialization. This way you can know what exactly is wrong.
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[[NSData alloc]
initWithContentsOfFile:jsonPath]
options:0
error:&error];
if (error)
{
NSLog(#"Could not serialize GeoJSON file: %#", [error localizedDescription]);
}
else
{
//GeoJSON has been successfully decoded
}

iOS Error Parsing JSON

I am new to iOS. I have this JSON data that I needed to parse:
{
"allseries":[
{
"type":"HR",
"title":"Heart Rate",
"xLabel":"Time",
"yLabel":"Beats per Min",
"defaultUnit":"BPM",
"url":"info/info?user=admin%40korrent.com&type=HR",
"size":18,
"firstTs":1406755651,
"lastTs":1406841254
},
{
"type":"TEMP",
"title":"Temperature",
"xLabel":"Time",
"yLabel":"Temperature",
"defaultUnit":"F",
"url":"info/info?user=admin%40korrent.com&type=TEMP",
"size":6,
"firstTs":1406854147,
"lastTs":1406854283
}
],
"status":"OK"
}
So far this is my code:
NSString *dataReceived= [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(#"--> async response data (string): %#", dataReceived);
NSData *jsonData = [dataReceived dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&jsonError];
NSLog(#"JSON key and value %#", [dict description]);
NSLog(#" %# ", dict[#"allseries"]);
NSString *jsonString=dict[#"allseries"];
if (_programState == 4){
NSLog(#"state is 4");
NSLog(#"%#",jsonString);
NSData *Data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
However, the code throws invalid argument exception for this line:
NSData *Data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
Further more, jsonString seems totally "inoperable". I cannot split it, append strings to it etc. So what's wrong?
// The following two lines are just for logging and otherwise are not needed.
NSString *dataReceived= [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
NSLog(#"--> async response data (string): %#", dataReceived);
// Deserialize the JSON data into a dictionary
NSError *jsonError;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: _responseData options:nil error:&jsonError];
NSLog(#"JSON key and value %#", dict);
// Get the array from the dictionary element "allseries".
NSArray *jsonArray = dict[#"allseries"];
NSLog(#"jsonArray: %#", jsonArray);
Here jsonArray is the array of dictionaries for "allseries" from the JSON
What you did is totally, totally wrong.
dict[#allseries] is not an NSString. It is an NSArray with two elements, and both its elements are NSDictionary.

NSJSONSerialization and Emoji

I'm currently trying to POST some JSON containing emojis to a python API. I tried feeding the NSJSONSerialization directly with the string containing the emojis from my UITextField but the serializer crashed with no meaningful explanation.
Afterwards I tried to do some format conversion and ended up with something like this:
NSString *uniText = mytextField.text;
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ;
This basically works except that the resulting UTF-8 is kinda double-"escaped" resulting in the following:
"title":"\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d"
Any suggestions how to fix that?
There are two difficulties:
1. Apple hosed NSString WRT UTF Planes 1 and above, the underlying use
of UTF-16 shows through. An example is that length will return 2 for
one emoji character.
2. Whoever decided to put emoji in Plane 1 was
just being difficult, it is the first use of Plane 1 and a lot of
legacy UTF code does not handle that correctly.
Example code (adapted from #Hot Licks):
Updated with OP emoji
NSString *uniText = #"💦💏👒👒💦";
NSDictionary* jsonDict = #{#"title":uniText};
NSData * utf32Data = [uniText dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"utf32Data: %#", utf32Data);
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
if (jsonData == nil) {
NSLog(#"JSON serialization error: %#", error);
}
else {
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"The JSON result is %#", jsonString);
NSLog(#"jsonData: %#", jsonData);
}
NSLog output
utf32Data: a6f40100 8ff40100 52f40100 52f40100 a6f40100
The JSON result is {"title":"💦💏👒👒💦"}
jsonData: 7b227469 746c6522 3a22f09f 92a6f09f 928ff09f 9192f09f 9192f09f 92a6227d
Sigh:
NSString* uniText = mytextField.text;
NSDictionary* jsonDict = #{#"title":uniText};
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJsonObject:jsonDict options:0 error:&error];
if (jsonData == nil) {
NSLog(#"JSON serialization error: %#", error);
}
else {
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"The JSON result is %#", jsonString);
}
If myTextField.text is a valid NSString then no other conversions should be required. NSJSONSerialization will provide all necessary "escaping".

With AFNetworking how can I read the error message contained in request.responseString

NSLog(#"%#", request.responseString);
This gives me output of {"errors":{"email":["is already taken"]}}.
I would like to save email and the message string "is already taken" into a string to display in an alert. How can I access those two items into two strings?
The response string is the raw output from the server. In this case it is JSON encoded. You can either use one of the AFNetworking JSON-specific classes (i.e. AFJSONRequestOperation) to get the response back as a JSON object, or parse it yourself using NSJSONSerialization. I would suggest using AFJSONRequestOperation.
NSData *data = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *str = [[json objectForkey:#"errors"] objectForKey:#"email"][0];
I used the following, seems a tiny bit more robust:
[requestOperation setCompletionBlockWithSuccess:success failure:^(AFHTTPRequestOperation *operation, NSError *error) {
id response = error.userInfo;
if (response && [response isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDictionary = (NSDictionary *)response;
// AFNetworking hides the actual error response under this key
if ([responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey]) {
id suggestedRecovery = [responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey];
if ([suggestedRecovery isKindOfClass:[NSString class]]) {
// Try to json decode string
id json = [NSJSONSerialization JSONObjectWithData:[suggestedRecovery dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
if (json && [json isKindOfClass:[NSDictionary class]]) {
responseDictionary = json;
}
}
}
// .. extract error message out of responseDictionary
}
}];
NSData *responseData = [[error userInfo] objectForKey:#"data"];
if ([responseData length] > 0)
{
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
}

Resources