English 中文(简体)
如何在iPad Apps教化JSON?
原标题:How to parse JSON string in iPad Apps?

我想要改变我数据库中的形象,以便用我的“智者”来检索它,并且为了做到这一点,我没有这样做。

Inside my button onTouch i have written this code

//create string contain url address for php file, the file name is phpFile.php,it receives parameter :name 
NSString *strURL=[NSString stringWithFormat:@"....my.php","hhhhh"];

//to execute php code
NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 

//to receive the returned Result 
NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]self]; 
NSLog(@"%@",strResult);
问题回答

There are different ways of doing this:

  1. If you deployment target is iOS 5.0 and up you could use: NSJSonSerialization
  2. if you want to support versions below 5.0 you could use one of many libraries to do that, personally I find SBJson quite good. Take a look at this snippet to get an idea :

    NSString *dataString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSDictionary *dic = [dataString JSONValue];
    NSArray * element = [dic objectForKey:@"jsonKey"];
    NSString * someSimpleElement = [dic objectForKey:@"jsonKey"];
    

如果你针对Si,或者后来,你可以使用NSJSONSerialization

NSError& anError = nil;
id foo = [NSJSONSerialization JSONObjectWithData: dataURL options: 0 error: &anError];
if (foo == nil)
{
    // handle the error
}
else
{
    //play with the returned object
}

如果你需要瞄准较早版本的SOS,那么你就需要SBJSON或TJSON或类似,但原则是相同的。

NB 还请读到,现行代码将服务器的数据同步输入。 如果这是主线,那么,在你获得数据或连接时间之前,调查股将予以冻结。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签