dragon4808 2013-03-25 08:22
浏览 87

在iphone应用程序中解析json数据

I am parsing data from server in json to use in iphone app in following it takes any search text field and post it server then matches the text and returs data below is my iphone code

    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);

Here is the result of JSOn string

       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}

My PHP code for url

     $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;
     }
  • 写回答

1条回答 默认 最新

  • doujiazong0322 2013-03-25 08:55
    关注

    As per your latest comment, the reason for this is that your query is failing.

    Firstly, you should not be using mysql_* functions. See the big red box here. Consider using PDO or MySQLi instead.

    Secondly, it looks like you may be leaving yourself open to SQL injection. You should be escaping your queries.

    Thirdly, you should be performing error checking on your query. Something similar to:

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

    This should give you an idea as to why the query is failing.

    You've also not posted the code for your mysql_connect(), you should also be error checking this. Something similar to:

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

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)