I have an XML file and I am converting the data into NSString using this code:-
NSString *strPath=[[NSBundle mainBundle] pathForResource:#"VISG" ofType:#"xml"];
NSData *data = [NSData dataWithContentsOfFile:strPath];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
The problem I am facing is I am getting the string with all the XMLValues but I am unable to convert that string into Dictionary. I have used XMLReader but it always returns me nil . I have tried the below two methods for converting the string which I ma getting from NSdata but it always throws nil
dict = [XMLReader dictionaryForXMLString:string
options:XMLReaderOptionsProcessNamespaces
error:&error];
dict = [XMLReader dictionaryForXMLString:string error:nil]
Can anyone please tell me what is the problem. Or can you please tell me some more libraries other than XMLReader for parsing the XMLfile.
Related
I am getting NSString * responseString as below format which is already utf8 encoded. Data is from webserver database.
NSString *jsonString = #"{\"ID\":{\"Content\":\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13,\"type\":\"text\"}}";
//Trying to convert to UTF
NSString *utf = [jsonString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//convert to data
NSData *data = [utf dataUsingEncoding:NSUTF8StringEncoding];
//trying to create NSDictionary JSON
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"json: %#", son);
NSLog(#"utf: %#", utf);
I NEED TO CONVERT IT INTO NSDICTIONARY SO I CAN PARSE AND GET Value of Content
Output Results:
utf: (null)
utf: {"ID":{"Content":ฉันรักคุณ,"type":"text"},"ContractTemplateID":{"Content":ฉันรักคุณ,"type":"text"}}
Strings will present as Unicode in debugger. Just show them with labels or convert into characters with some online tools.
I am trying to send an NSMutableArray using custom URL. Below is the code to do send:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)jsonString,
NULL,
(CFStringRef)#"<>!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
I am able to send the encoded string in custom url and access it via [url query] followed by url decode. My problem is that I am able to get contents similar to what is in "jsonString" but afterwards I am not getting how to retrieve the array from it. Can someone guide to the right direction.
Thanks
Good afternoon,
I'm trying to store a "text" using NSString from my JSON output but it's not working because it's always NULL. It's working fine with the other info (because they are integers) but when I have to store the info (the picture) in a NSString it's always NULL.
I have tried to search a lot of places in order to see how I have to declare that "NSString" and make it work but it's not working and I need some help.
Can you help me with that? How can I fill the NSString with the information in "picture" from my JSON output?
ProfileViewController:
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSString *picture = [jsonData objectForKey:#"picture"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picture]];
self.profileimage.image = [UIImage imageWithData:imageData];
JSON output:
{"success":1,"stars":0,"photos":0,"followers":0,"picture":http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg}
Thanks in advance.
The code looks correct, however the JSON output is not valid according to http://jsonlint.com/
You need to wrap the link with quotes ("http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg") and then try again.
How to convert NSDictionary to NSString which contains JSON of NSDictionary ?
I have tried like but without success
//parameters is NSDictionary
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:0
error:&error];
if jsonData is NSDictionary
NSString *str=[NSString stringWithFormat:#"json data is %#", jsonData];
OR if jsonData is NSData
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
If you just want to inspect it, you can create a NSString:
NSString *string = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
But if you're writing it to a file or sending it to a server, you can just use your NSData. The above construct is useful for examining the value for debugging purposes.
In my iOS 5.1 application, I'm trying to load a JSON text file into a NSDictionary.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"stuff" ofType:#"json"];
NSDictionary *stuff = [NSDictionary dictionaryWithContentsOfFile:filepath];
The filepath is valid, the file exists, but stuff is nil, which means there was a problem decoding the file (according to the documentation).
(How) Can I get more debugging information on why [NSDictionary dictionaryWithContentsOfFile:] - or any kind of API call - returns nil?
Read the contents as NSData and then use +[NSPropertyListSerialization propertyListWithData:options:format:error:].
As you said: Your file contains a JSON string. First, you have to load the string and then you have to parse it using some JSON library.
E.g.:
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString: content];