impossible to parse a url containing "and space in xcode - ios

I'm trying to parse json in a url like:
NSError *error = nil;
NSString *sampleUrl= #"http://xbmc:xbmc#192.168.1.23:8080/jsonrpc?request={\"jsonrpc\":%20\"2.0\",%20\"method\":%20\"VideoLibrary.GetMovies\",%20\"params\":%20{%20\"filter\":%20{\"field\":%20\"playcount\",%20\"operator\":%20\"is\",%20\"value\":%20\"0\"},%20\"limits\":%20{%20\"start\"%20:%200,%20\"end\":%2075%20},%20\"properties\"%20:%20[\"art\",%20\"rating\",%20\"thumbnail\",%20\"playcount\",%20\"file\"],%20\"sort\":%20{%20\"order\":%20\"ascending\",%20\"method\":%20\"label\",%20\"ignorearticle\":%20true%20}%20},%20\"id\":%20\"libMovies\"}";
NSLog(#"%# ",sampleUrl);
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sampleUrl]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
but I get an error saying that:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
When I copy my url in my browser: it works!
but not when I tried with xcode
I tried changing the setting "by 22% and eliminating \ : does not work!
how do I proceed?
thanks

The problem is that the string in sampleUrl is not a well formed URL (see RFC 1738).
It contains various characters that are not allowed in URLs, for example { and ". These character have to be percent-escaped before converting to an NSURL.
Where does your URL string really come from?

here is the result that works wonders:
NSString *urlString = [NSString stringWithFormat:#"http://xbmc:xbmc#192.168.1.23:8080/jsonrpc?request={\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.GetMovies\", \"params\": { \"filter\": {\"field\": \"playcount\", \"operator\": \"is\", \"value\": \"0\"}, \"limits\": { \"start\" : 0, \"end\": 75 }, \"properties\" : [\"art\", \"rating\", \"thumbnail\", \"playcount\", \"file\"], \"sort\": { \"order\": \"ascending\", \"method\": \"label\", \"ignorearticle\": true } }, \"id\": \"libMovies\"}"];
NSLog(#"%# ",sampleUrl);
NSString *encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:encodedUrl];
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
Thank you to you all for the help;)

Related

-[__NSSingleObjectArrayI dataUsingEncoding:] Objective C

My JSON string is
"{\"CommentList\":[{\"SubmittedDate\":\"02/01/2017 06:09:32\",\"SubmittedTime\":\"\",\"UserId\":\"af0e1cda5c0\",\"UserName\":\"NepolionBon\",\"Comments\":\"\",\"Complete_Hour\":\"\",\"Complete_Minute\":\"\"}]}"
I need value of "SubmittedDate" and "Complete_Hour" from it.
When I'm trying to convert this string with
NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
getting an error saying
-[__NSSingleObjectArrayI dataUsingEncoding:]: unrecognized selector sent to instance 0x14decfc0
can anyone help me?
You can use this code to get SubmittedDate and Complete_Hour.
NSString *jsonString = #"{\"CommentList\":[{\"SubmittedDate\":\"02/01/2017 06:09:32\",\"SubmittedTime\":\"\",\"UserId\":\"af0e1cda5c0\",\"UserName\":\"NepolionBon\",\"Comments\":\"\",\"Complete_Hour\":\"\",\"Complete_Minute\":\"\"}]}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray* arrComments = [json objectForKey:#"CommentList"];
for (NSDictionary *dict in arrComments) {
NSLog(#"Submit date :%#",[dict objectForKey:#"SubmittedDate"]);
NSLog(#"Comlplete Hour : %#",[dict objectForKey:#"Complete_Hour"]);
}

Objective-C JSON text did not start with array or object and option to allow fragments not set

I am trying to get XML From a web services doing the following:
NSString *areaDescriptionWSpaceCharacters = [areaDescription componentsJoinedByString:#","];
areaDescriptionWSpaceCharacters = [areaDescriptionWSpaceCharacters stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *requestString = [NSString stringWithFormat:#"%#?areaDescriptionPLXml=%#",kIP,areaDescriptionWSpaceCharacters];
NSURL *JSONURL = [NSURL URLWithString:requestString];
NSURLResponse* response = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
if(data == nil)
return nil;
NSError *myError;
NSDictionary *punchList = [[NSDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
but I get this error:
[0] (null) #"NSDebugDescription" : #"JSON text did not start with array or object and option to allow fragments not set." for `myError`
Here is my XML:
<ArrayOfKeyValueOfstringPunchListCellModel84zsBx89>
<KeyValueOfstringPunchListCellModel84zsBx89>
<Key>ORC0023</Key>
<Value>
<baseOrSchedStartList>
<string>2015-09-11T08:00:00</string>
<string>2015-08-10T16:00:00</string>
<string>2015-08-11T16:00:00</string>
</baseOrSchedStartList>
</Value>
</KeyValueOfstringPunchListCellModel84zsBx89>
</ArrayOfKeyValueOfstringPunchListCellModel84zsBx89>
What am I doing wrong?
Your XML is... an XML. Not a JSON.
Try using a 3rd party such as this:
https://github.com/nicklockwood/XMLDictionary
for XML parsing.

How to parse {status:0,val:450} from a HTML response

i want to take 450
from the following:
NSString {status:0,val:450}
using objective c:
{status:0,val:450}
Please suggest me answer
I cannot completely understand your question . I think you have you have an JSON data like:
{
status:0;
val:450;
}
If you want this data in your app. You want to do
NSURL *blogURL = [NSURL URLWithString:#"https://your url here"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data = [dataDictionary objectForKey:#"status"];
NSDictionary *item = [data objectForKey:#"val"];
Your JSON Data get in Dictionary format. You can access the Data using the Key. In here your keys are status and val.
I hope this links are help for you.
fetch parse json ios programming tutorial
and this Link:
json parsing in ios

Null JSON response

NSString *connection = #"http:"(link);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:connection];
NSString *json = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSLog(#"\nJSON: %# \n Error: %#", json, error);
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%#",jsonData);
NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSLog(#"JSON: %#", jsonDict);
}
});
The above code returns :
JSON: "{\"value\":[{\"Id\":\" }}
but
NSData is 69746963 735c222c 5c224973 41767469 76655c22 3a5c2231 5c222c
and jsonDict returns (null).
I am confused what may be going on.
Can somebody please help me figure it out.
I am trying to get the JSON data but it returns null.
Thanks
You say the server gave you this:
JSON: "{\"value\":[{\"Id\":\" }}
If I remove the backslashes that were added by the NSLog command, the actual JSON that you received was
{"value":[{"Id":" }}
That isn't valid JSON. That's all there is to it; the server sent you rubbish data, and there is no way to get any information from it. Contact the people creating the server software and ask them to fix the problem.
And you really, really need to distinguish between what an object really contains, and what NSLog prints. NSLog prints the contents of an NSData object by displaying each byte as two hexadecimal digits.
Try one of these (array or dictionary) without converting it into JSON, depending on what u want :
NSString *connection = #"http://(link)";
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSDictionary *jsonDict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:connection]];
NSArray *jsonArray = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:connection]];
NSLog(#"DICT: %# ARRAY: %#", jsonDict, jsonArray);
});
I haven't tried it, please paste results?
By the look of your NSString response, it starts with { and ends with } which means it is a NSDictionary.
Change :
NSMutableArray *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
to :
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&error];

XML to JSON conversion in ios

i'm trying to convert a simple xml document in Xcode to JSON. The problem is keep returning nil.
This is my code:
NSURL *url = [[NSURL alloc] initFileURLWithPath:#"http://www.w3schools.com/xml/note.xml"];
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:url];
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:xmlData error:&parseError];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary
options:NSJSONWritingPrettyPrinted
error:&error];
NSLog(#"%#", jsonData);
Error message:
[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil'
As rmaddy said, try printing parseError. If parseError is Nil, then try printing the xmlDictionary, and check if the dictionary is created correctly.
If xmlDictionary is also Nil, then check the xmlData, which should be fine, in which case the dictionaryForXMLData method must be verified if it checks for any conditions.
Hope I helped.

Resources