解析json数据在iphone应用程序


Parsing json data in iphone application

我解析数据从服务器在json在iphone应用程序中使用以下它需要任何搜索文本字段和发布它的服务器然后匹配文本和返回数据下面是我的iPhone代码

    NSString*searchText=searchTextField.text;
NSString *post =[[NSString alloc] initWithFormat:@"searchCode=%@",searchText];
NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/searchCatalog.php?"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    NSData* myData=[data dataUsingEncoding:NSUTF8StringEncoding];
    NSString *json_string = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
    NSArray *dataArr=[json_string JSONValue];
    for (int i=0; i<[dataArr count]; i++) {

        if (!dataArr || !dataArr.count){

            if(resultArray!=nil){
                resultArray=nil;
                resultArray=[[NSMutableArray alloc]init];
            }


        }

        NSDictionary *dict=[dataArr objectAtIndex:i];
        ObjectData *theObject =[[ObjectData alloc] init];

        [theObject setCategory:[dict objectForKey:@"category"]];
        [theObject setSub_Category:[dict objectForKey:@"sub_Category"]];    
        [theObject setContent_Type:[dict objectForKey:@"content_Type"]];
        [theObject setContent_Title:[dict objectForKey:@"content_Title"]];
        [theObject setPublisher:[dict objectForKey:@"publisher"]];
        [theObject setContent_Description:[dict objectForKey:@"content_Description"]];
        [theObject setContent_ID:[dict objectForKey:@"content_ID"]];
        [theObject setContent_Source:[dict objectForKey:@"content_Source"]];


        [resultArray addObject:theObject];
        [theObject release];
        theObject=nil;

    NSLog(@"%@", json_string);
这是JSOn字符串 的结果
       ProductivoApp[2087:c203] -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 '"Unrecognised leading character'" UserInfo=0x57b5a10 {NSLocalizedDescription=Unrecognised leading character}

url的PHP代码

     $flu=$_POST['searchCode'];

       $query =mysql_query("SELECT * From catalog_Master WHERE category_Title LIKE '%$flu%'");
    $rows = array();
    while($row = mysql_fetch_assoc($query)) {
    $rows[] = $row;
     }

根据你最近的评论,这样做的原因是你的查询失败。

首先,您不应该使用mysql_*函数。看到这里的大红框了吗?考虑使用PDO或MySQLi。

其次,看起来您可能会让自己对SQL注入敞开大门。你应该转义你的查询。

第三,应该对查询执行错误检查。类似于:

if(!$query) {
   die('Query failed. ' . mysql_error()');
}

这应该让你知道为什么查询失败。

你也没有张贴你的mysql_connect()的代码,你也应该错误检查这个。类似于:

$link = mysql_connect('localhost', 'user', 'pass');
if(!$link) {
   // Handle it
}