網頁

2012年3月22日 星期四

iOS5 JSON

iOS5 - Advanced JSON using Bing API plist

[MAC]MAMP架站懶人包輕鬆架設Apache|MySQL|PHP


Using JSON to Access MySQL from an iPhone. http://php.net/manual/en/function.json-encode.php


http://php.refulz.com/json-with-php-json-encode-and-decode/


    NSString *jsonString = @"{\"status\":\"200\",\"id\":\"23\",\"username\":\"nipinponmudi@gmail.com\"}";
    SBJsonParser *parser = [SBJsonParser new];
    id object = [parser objectWithString:jsonString];
    
    NSString *test = [object objectForKey:@"username"];
     NSLog(@"JSON in Xcode: %@",test);
===================================================
json file php file
  
 "Google", "website" => "www.google.com"), 
array("name" => "Microsoft", "website" => "www.microsoft.com"), 
array("name" => "Yahoo", "website" => "www.yahoo.com"));

$jsonString =  json_encode($companies);

//echo $jsonString;

 echo '{"users":'.$jsonString.'}';

/* Output:[{ "name":"Google","website":"www.google.com"},
{"name":"Microsoft","website":"www.microsoft.com"}, 
{"name":"Yahoo","website":"www.yahoo.com"}] */



?>

json file server side output
  
{"users":[{"name":"Google","website":"www.google.com"},{"name":"Microsoft","website":"www.microsoft.com"},{"name":"Yahoo","website":"www.yahoo.com"}]}


parse json for iPhone



Xcode parse sample
   
    self.title = @"SBJSON";
    
    jsonURL = [NSURL URLWithString:@"http://xxx/jsonarray.php"];
       
    NSString *jsonReturn = [[NSString alloc] initWithContentsOfURL:jsonURL encoding:NSUTF8StringEncoding error:nil];
    
     // Parse the string into JSON 
    self.jsonArray = [jsonReturn JSONValue];
    NSLog(@"jsonfile %@",jsonArray);
    
    SBJsonParser *parser = [[SBJsonParser alloc] init];
   
//    NSDictionary *jSON = [parser objectWithString:jsonReturn error:nil];  // json-->NSDictionary
//    NSString *users = [jSON  objectForKey:@"users"]; 
//    NSLog(@"jsonFind %@",users);
//    
//    NSString *usersItems = [users  valueForKey:@"name"]; 
//    NSLog(@"jsonFindItem %@",usersItems);
    
    NSDictionary *jSONOne = [parser objectWithString:jsonReturn error:nil];  // json-->NSDictionary
    NSArray *usersOne = [[jSONOne objectForKey:@"users"] objectAtIndex:0] ;
    NSLog(@"jsonFind %@",usersOne);
    
    NSArray *usersOneItem = [usersOne valueForKey:@"name"]  ;
    NSLog(@"jsonFindItem: %@",usersOneItem);

Xcode parse sample output
   
2012-03-30 11:41:21.139 SBJSON[4500:207] jsonfile {
    users =     (
                {
            name = Google;
            website = "www.google.com";
        },
                {
            name = Microsoft;
            website = "www.microsoft.com";
        },
                {
            name = Yahoo;
            website = "www.yahoo.com";
        }
    );
}
2012-03-30 11:41:21.142 SBJSON[4500:207] jsonFind {
    name = Google;
    website = "www.google.com";
}
2012-03-30 11:41:21.143 SBJSON[4500:207] jsonFindItem: Google

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。