here is my code and It doesn't work
NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.bbblllaaahhh.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.city = [[NSMutableArray arrayWithArray:[string componentsSeparatedByString:#"\""]] JSONValue];
And here is JSON text
[
{
"kanji_name":"\u30ac\u30fc\u30c7\u30f3\u30d5\u30a3\u30fc\u30eb\u30ba\u3000\u3068\u306d\u308a\u516c\u5712BigBell"
}
]
It reports in self.city line , What should I do ??
Yeah!! I completely fix it Here is My fix code
NSError *theError = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.blahblah.com"]];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
NSArray *jsonArray = [NSArray arrayWithArray:(NSArray *)[jsonDict valueForKey:#"kanji_name"]];
NSMutableString *text = [[NSMutableString alloc] init];
[text appendFormat:#"%#",[jsonArray objectAtIndex:0]];
self.city = [NSMutableArray arrayWithObject:text];
A couple of questions will be needed to help determine the answer to this problem.
First, can you place
NSLog(#"%#",data);
after the first line and
NSLog(#"%#", string);
after the second line and tell us what value it reports to the console? This will help determine if the problem is
1) That the server never returns any data or returns the wrong data and
2) If the data is correctly turned into a string. If either of these actions fails, it may cause an error in line 3.
Next, could you report what error the third line is giving? There are many possible problems. It could be that the string is not, in fact, proper JSON code and the JSON parser is crashing.
Line 3 looks like it has one obvious problem. First, you are splitting a string based on the "\" character, which seems like an unusual thing to do in this situation. But anyway, the order of operations will look like this:
#"a\b"
will be turned into
["a", "b"]
then the JSON parser will try to parse ["a", "b"], which will certainly cause an error.
At the very least, you will want to do something like,
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray or NSDictionary *parserResults = [string JSONValue];
// This will depend on what the actual JSON string returned from the server
// With an array, maybe something like
NSString *stringWithBackslash = [NSArray objectAtIndex:0];
// With a dictionary, maybe something like
NSString *stringWithBackslash = [NSDictionary objectForKey:#"backslashString"];
self.city = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:#"\""]];
Is self.city an NSMutableArray? The variable name makes it sound like it should be a string. In that case, you would actually want to do something like
NSMutableArray components = [NSMutableArray arrayWithArray:[stringWithBackslash componentsSeparatedByString:#"\""]];
// if the city is the first element of the array
self.city = [components objectAtIndex:0];
You will also want to check to make sure that components, for example, has more than element because that also might cause an error, if, for example, the server returned an error or there were no internet connection.
Related
Alright, so here's where I'm at with my app.
I've got some code that connects to a remote server and grabs some XML data, and forms it into a string. Here is the code below:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"GET"];
NSString *accept = [NSString stringWithFormat:#"application/xml"];
[request addValue:accept forHTTPHeaderField: #"Accept"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString* responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(#"%#",responseString);
Which returns
That's exactly the information I want. My question is how I can get the double value out of the gold brackets and into my application to use.
Thanks a lot!
There are a lot of tutorials of how to use NSXMLParser. For example, ray wenderlich or here and here.
There's quite a few XML parsing options available - ranging from libxml2, to NSXMLParser and a host of open-source projects.
One of my favorite XML parsers is RaptureXML, which can be installed via CocoaPods.
I like to create a category on RXMLElement to parse as follows:
- (PFCurrentConditions*)asCurrentCondition
{
NSString* summary = [[self child:#"weatherDesc"] text];
PFTemperature* temp = [PFTemperature temperatureWithFahrenheitString:[[self child:#"temp_F"] text]];
NSString* humidity = [[self child:#"humidity"] text];
NSString* wind =
[NSString stringWithFormat:#"Wind: %# km %#", [[self child:#"windspeedKmph"] text], [[self child:#"winddir16Point"] text]];
NSString* imageUri = [[self child:#"weatherIconUrl"] text];
return [PFCurrentConditions conditionsWithSummary:summary temperature:temp humidity:humidity wind:wind imageUrl:imageUri];
}
So, you can then use it as follows:
RXMLElement* rootElement = [RXMLElement elementFromXMLData:response.responseData];
__autoreleasing PFWeatherReport* weatherReport = [rootElement asWeatherReport];
Again, this is one of countless options, but my personal favorite.
I have a String that I got from a webserver which came in json format, but the string is huge with everything in it. I tried using the NSDICTIONARY but to no success. I was wondering what would be the best approach to break this string and add to different strings and eventually put it all in a class of strings. Thanks for the help! Here is my code:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:#"http://mym2webdesign.com/meiplay/paulsuckedabuffalo/artists.php"]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSError *error=nil;
NSLog(#"HHHHHHHHHHHHHH"); //use this to know how far Im getting
NSLog(returnString); // Look at the console and you can see what the restults are
/*NSDictionary *results = [returnString JSONValue];
NSString *ID = [results objectForKey:#"ID"]; // for example
NSLog(#"ID Number: %#", ID);*/
Here is some of the log i get:
[{"ID":"1","name":"kevin","bio":"kevins bio"},{"ID":"1","name":"kevin","age":"20"},{"ID":"2","name":"Cesar","bio":"Cesar bio"},{"ID":"2","name":"Cesar","age":"19"},{"ID":"3", "name":"Katherine", "bio":"Katherines bio"},{"ID":"3", "name":"Katherine", "age":"22"}]
You are doing it wrong. Its a NSArray of NSDictionaries. So first you need to assign it to NSArray and then loop over it to get each individual NSDictionary. See below.
NSArray *results = [returnString JSONValue];
for(NSDictionary *record in results)
{
NSLog(#"ID: %#", [record objectForKey:#"ID"]);
}
You'll probably be better off just using NSJSONSerialization if your app is targeted for at or over iOS 5.0:
NSArray *JSONArray = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&error];
You might need to experiment with using NSArray vs. NSDictionary, etc., but this should be an overall simpler solution.
Try this :
NSArray *results = [returnString JSONValue];
for (int i=0; i<[results count];i++) {
NSDictionary *DetailDictonary=[results objectAtIndex:i];
NSString *strid=[DetailDictonary objectForKey:#"ID"];
NSString *strName=[DetailDictonary objectForKey:#"name"];
NSString *strBio=[DetailDictonary objectForKey:#"bio"];
// Or You can set it in Your ClassFile
MyClass *classObj=[[MyClass alloc] init];
classObj.strid=[DetailDictonary objectForKey:#"ID"];
classObj.strName=[DetailDictonary objectForKey:#"name"];
classObj.strBio=[DetailDictonary objectForKey:#"bio"];
[YourMainArray addObject:classObj]; //set YourClass to Array
[classObj release];
}
My code:
NSString *jsonString;
jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];
NSLog(#"%#",jsonString);
// Build an array from the dictionary for easy access to each entry
NSArray *places = [results objectForKey:#"description"];
I am not getting the result i wanted.
When I debug the code I am getting 2 key/value pair & 0 object for NSArray places.
It is correct that you have 2 key/value pairs. The top two keys intact are "predictions" and "status". So you must first of all extract predictions (it's an array):
NSArray *predictions = [results objectForKey:#"predictions"];
and then iterate over it; also note that "description" is a string and to get the places you must split it using the "," separator:
for(NSDictionary *aPrediction in predictions) {
NSString *description = [aPrediction objectForKey:#"description"];
NSArray *placesInDescription = [description componentsSeparatedByString:#","];
}
maybe you should use:
NSArray *places = [results valueForKey:#"predictions"];
Works for me without any problems with JSON Kit!
NSString *jsonString;
NSData *responseData = [NSData dataWithContentsOfURL: [NSURL URLWithString:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=solapur&sensor=true&key=AIzaSyC0K5UhV_BWmXhncIZEnbh-WG2RVQVgfdY"]];
jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// Create a dictionary from the JSON string
NSDictionary *results = [jsonString objectFromJSONString];
// Build an array from the dictionary for easy access to each entry
NSArray *places = [results valueForKey:#"predictions"];
NSLog(#"places %#", places);
Never touched json before. I'm trying to access some variables within the Wunderground weather API for Melbourne. For example, let's say I want to access the "wind_dir":"East" variable. This is my code thus far:
NSString *urlString =
[NSString stringWithFormat:
#"http://api.wunderground.com/api/key/geolookup/conditions/forecast/q/-33.957550,151.230850.json"];
NSLog(#"URL = %#", urlString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *weatherInfo = [parser objectWithString:json_string error:nil];
for (NSDictionary *weatherString in weatherInfo)
{
NSLog(#"some weather info = %#", [[[weatherString objectForKey:#"response"] objectForKey:#"current_observation"] objectForKey:#"wind_dir"]);
}
My code reaches the for loop and crashes with this error: -[NSCFString objectForKey:]: unrecognized selector sent to instance.
I'm not 100% sure what's causing the crash, and whether my path to the "wind_dir" variable is correct, though they could well be the same problem.
Thanks in advance for any help.
either the "response" property or the "current_observation" propery is string and not dictionary.
the error you are getting is that you are trying to call "objectForKey" on a string.
after looking at the result of the API, it seems that you are not getting an array.
You should do something like this:
NSDictionary *weatherInfo = [parser objectWithString:json_string error:nil];
NSLog(#"some weather info = %#", [[weatherInfo objectForKey:#"current_observation"] objectForKey:#"wind_dir"]);
instead of your for statement.
I have an iPhone app which sends a request to a url posting a variable called submit:
+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url{
NSMutableArray *questions = [[NSMutableArray alloc] init];
//connect to database given by url
//NSError *error = nil;
//NSURLResponse *response = nil;
NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:#"section=%#", section];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPMethod: #"POST"];
//post section
[request setHTTPBody: myRequestData];
//store them in the array;
return [questions autorelease];
}
My php file:
<?php
//connect to database
function connect() {
$dbh = mysql_connect ("localhost", "abc1", "12345") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
//store posted data
if(isset($_POST['section'])){
$dbh = connect();
$section = $_POST['section'];
$query = mysql_query("SELECT * FROM QUESTIONS WHERE sectionId = $section;") or die("Error: " . mysql_error());;
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
}
echo '{"questions":'.json_encode($rows).'}';
mysql_close();
}
?>
I have built a model class (Question) in objective c which has the exact properties that each row element has in the rows associative array.
My questions are:
1) How can I read the echo'd JSON array elements and their relative attributes in objective C?
2) How can I create an array of Question objects and map each one to an element in the rows array?
3) What do I have to write in my method "+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url" to capture the reply from the php (the echo)?
EDIT:
Here is the output of the php:
http://dev.speechlink.co.uk/David/get_questionstest.php
UPDATE
Changed method to use ASIHTTPRequest - Cannot deserialise JSON string:
//method to
+(NSDictionary*)getQuestions:(NSString*)sectionId from: (NSString*) url{
NSDictionary *questions;
NSURL *link = [NSURL URLWithString:url];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:sectionId forKey:#"section"];
NSError *error = [request error];
[request startAsynchronous];
if (!error) {
//NSString *response = [request responseString];
//store them in the dictionary
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
questions = [json objectFromJSONString];
NSLog(#"Data: %#", questions); //outputs Data: (null)
[json release];
[request release];
}else{
//UIAlertView to warn users there was an error
}
return questions;
}
Well, lets go through this one step at a time.
You can create a NSDictionary from JSON quite easily by using one of several different JSON parsing libraries. I really enjoy using JSONKit. Once you've imported JSONKit, into your project, you can do something like this:
NSString *url = #"http://dev.speechlink.co.uk/David/get_questionstest.php";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *questions = [json objectFromJSONString];
[json release];
Now you have an array filled with the questions in your example. Now you can loop through this array and fill your data with the data in the array. Now lets be practical. It would be easier if you just had to manage one object instead of four for each question, wouldn't it? Lets make a class that contains one question each instance.
Interface:
#interface Question : NSObject {
NSString *questionId;
NSString *question;
NSString *questionNumber;
NSString *sectionId;
}
#property(copy)NSString *questionID;
#property(copy)NSString *question;
#property(copy)NSString *questionNumber;
#property(copy)NSString *sectionId;
#end
And implementation:
#implementation Question
#synthesize questionId, question, questionNumber, sectionID;
#end
Now that's just a basic example. Nothing fancy. Now you can loop through the array you had before and create "question" objects that contain each question's data. For my purposes, suppose you have a NSMutableArray named questionsArray that contain the questions you want to use. We'll loop through the dictionary and add the questions from the dictionary into the questionsArray array.
for (NSDictionary *q in questions) {
/* Create our Question object and populate it */
Question *question = [[Question alloc]init];
question.questionId = [q objectForKey:#"questionId"];
question.question = [q objectForKey:#"question"];
question.questionNumber = [q objectForKey:#"questionNumber"];
question.sectionId = [q objectForKey:#"sectionId"];
/* Add it to our question (mutable) array */
[questionsArray addObject:question];
[question release];
}
Tada! Now you have an array filled with Question objects. Any time you want to look at a property on any of the question objects, you can just simply access that property. For example, to grab the first question's number, you can just do this:
NSString *q1Number = [questionsArray objectAtIndex:0].questionNumber;
Please note this is all untested, as I don't have my compiler with me. It should get you started, though. =)
Edit: You were doing your request completely wrong. Try this:
+(NSDictionary*)getQuestions:(NSString*)sectionId from: (NSString*) url{
NSDictionary *questions = nil;
NSURL *link = [NSURL URLWithString:url];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:sectionId forKey:#"section"];
NSError *error = [request error];
[request startSynchronous];
if (!error) {
NSData *response = [request responseData];
NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
questions = [json objectFromJSONString];
[json release];
} else{
//UIAlertView to warn users there was an error
}
[request release];
return questions;
Take a look at Stig Brautaset's excellent JSON parser at GitHub.
And there are even a couple of sample projects included.
From your PHP, one would expect the parser to produce and array of NSDictionary objects. I'm not sure what you mean by question 2, but you can then iterate through the array and create custom "Question" objects with the NSDictionary values.
Hope this helps.
Didn't see that you added a third question. This is answered in the "TweetStream" example above. I would suggest that you use the NSURLConnectionDelegate methods as described by Apple.