How to use JSONValue with NSMutableArray
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"]];
NSMutableStri开发者_开发技巧ng *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.
精彩评论