Not able to Parse json string using SBJsonParser - iphone

my Json string is "{'message':'Test Driver'}"
if i try to Nslog this code NSLog(#"%#",[parser objectWithString:[request responseString] error:nil]);
parser is an object of SBJsonParser class
Output: (null)
is there any other json library which can parse this string?
plz help to parse this Json string

The JSON response string is not a valid one, that's the reason why you are getting the error.
Use the following link to check if the JSON string, sent as response from Web services is valid one or not
JSONLint

the raw JSON string should be in such a format
{"message":"Test Driver"}

As for other JSON libraries, if you're targeting iOS 5 or newer, Apple provides NSJSONSerialization.

you van use following code to generate appropriate json string and than parse it. It may help you
NSDictionary *resDict = [parser objectWithString:[[request responseString] stringByReplacingOccurrencesOfString:#"'" withString:#"\""] error:nil];

Related

SOAP request response giving undesired result

I am trying to fetch response from SOAP request.Response is an xml data.When i try to convert data into String it shows <,> symbols as '&gt' and '&lt' with ';' at end.Then the xml parser is not able to parse it
Check out this NSString category for HTML.
It adds several functions to NSString which you may find useful, like:
- (NSString *)stringByDecodingHTMLEntities;
You can run this function on your string and decode the HTML entities before giving the string to the XML parser.

Objective-C: Parsing a non-xml website

I'd like to develop a very simple "bus timetable" app that loads a particular URL and can use the HTML to figure out bus numbers, their routes and expected times. Unfortunately it's a very simple website, so the data is spread across TD and DIV fields, rather than in xml.
Can anyone provide some pointers on where to start? I've had a look at NSURL, NSURLConnection and the like, and am able to download the contents of an HTML file, but I'm unsure what to do next.
Many thanks.
There are many ways to accomplish this task. Ultimately, you will want to retrieve something an easily-processed format other than raw html. Here is one way to do it:
I would recommend writing a server-side script that converts the html into an easily-digestible format, such as JSON.
If you have php experience, you write a script / web-service that grabs the needed elements and place the content in an associative array. Place the script on your server and have your application call the web-service URL when ready to retrieve the info.
Finally, return the information as a JSON object and parse the JSON into your app.
<?php
//parse html into the $array variable.
$array = json_encode($array);
echo $array;
?>
I would recommend using the SBJSON library for parsing the JSON object: Take a look at SBJSON
Initiate your NSURL request by calling the script URL.
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.yoursite/script.php"]];
If the request is successful, parse the resulting JSON: (assuming use of SBJSON library)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[[SBJSON alloc] init]autorelease];
NSDictionary *busData = [json objectWithString:responseString error: &error];
//busData contains bus data that you formatted...
Beware, this is assuming that you want to add your data to a dictionary and then display it appropriately. I consider this a mere snippet for formatting data in a way you desire. Parsing all of the html within the app may difficult and that is why I advocate formatting the data as JSON first. If you decide to go the XML route, there are several XML parsers as well.

How to fetch web service response as a string in iPhone?

I've webservice which converts CelsiusToFahrenheit. This webservice response is in string format instead of xml so how can I display a response in a label programmatically?
Is there a sample available for that?
The simplest method is a one-liner:
NSError *error = nil;
myLabel.text = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
As with most one-line solutions, there's a caveat. This method blocks the current thread until the request completes or fails, so you'd better use it in conjunction with performSelectorInBackground:withObject: to avoid locking UI.
Here is a tutorial that I used to retrieve data from a web service using the requestWithURL method of NSURLRequest:
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
The example is for JSON, but you can just omit all of the JSON stuff from the tutorial, since the string response is going to be fed into your connectionDidFinishLoading method.

converting JSON object to nsdictionary from nsstring on iphone

We are trying to convert the JSON object from NSSTRING TO NSDICTIONARY but we are not getting any value. Here is the url for our webservice http://ecreeds.info/theappsoluteguideto/appservice.asmx/appArea
any help would be appreciated.
Use JSONLint.com it appears your webservice returns a 500 error for the response hence why you are not seing anything, because of invalid json
Your JSON isn't valid. In fact, it's quite off.

iPhone NSURLConnection: What to do with returned NSData Object? Google Docs API

I'm working with the Google Docs API in my iPhone application. I've sent the proper POST request, and received a response along with some NSData. If I NSLog the NSData object, I get this:
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:theResponse error:NULL];
NSLog(#"%#",returnedData);
//output : <4572726f 723d4261 64417574 68656e74 69636174 696f6e0a>
I think the NSData is either an NSDictionary or an NSArray. I'm supposed to receive a couple of items back, an SID, an LSID, and an Auth.
If I could turn the NSData chunk into an NSDictionary I could just find the object for whichever key.
Can anyone help?
You will have to decode the data. Try the following:
NSString *result= [[NSString alloc] initWithData:returnedData encoding:NSASCIIStringEncoding];
Of course this will depend on how the data is encoded. NSUTF8StringEncoding might work instead.
You may just need to pass the data to an NSXMLParser
The best way to deal with web data is Doing NSXML parsing. No need of decoding data the parser will handle itself