PHP / IOS:为网络服务编码json的最佳方法是什么?

时间:2022-10-26 20:08:37

I am trying to create a web service that serves up json from a mysql database through php for display in an iPhone app.

我正在尝试创建一个Web服务,通​​过php从mysql数据库提供json,以便在iPhone应用程序中显示。

I have a standard mysql/php setup.

我有一个标准的mysql / php设置。

The data is in a table with fields and records. It is queried with sql to create a record set. Each record in the recordset is a row.

数据位于包含字段和记录的表中。用sql查询创建记录集。记录集中的每条记录都是一行。

php

$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());  
$tasks = array();
while($row = mysql_fetch_assoc($res)) {
$tasks[] = array('row'=>$row);
 } 
echo json_encode(array('tasks'=>$tasks));
//

The web service produces the following output:

Web服务生成以下输出:

{"tasks":[{"row":{"userid":"1","task":"send email to Bob","longtask":"include attached memo"}}]}

However, I'm having a lot of trouble getting this to read into IOS suggesting that there might be a better format for the web service.

但是,我在阅读IOS时遇到了很多麻烦,这表明可能有更好的Web服务格式。

The structure is different from that in tutorials and other sources I have found for reading the json into IOS (none of which use php/mysql).

结构不同于我在教程中找到的用于将json读入IOS的其他资源(其中没有一个使用php / mysql)。

Can anyone tell me a better way to structure the json or alternatively code to read this json in iOS to grab the userid, task and other variables.

任何人都可以告诉我一个更好的方法来构建json或者代码来读取iOS中的这个json以获取用户ID,任务和其他变量。

When I try this, I get an error that it cannot rad row at index:0

当我尝试这个时,我得到一个错误,它不能在index:0处的rad行

      NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                         options:kNilOptions 
                                                           error:&error];
    NSLog(@"about to print json: %@",json);
NSMutableArray *getElement = [json objectForKey:@"tasks"];
    for (NSDictionary *dict in getElement) {
        NSArray *array = [dict objectForKey:@"row"];
        NSString *str = [array objectAtIndex:0];
    }

Thanks in advance for any suggestions.

在此先感谢您的任何建议。

1 个解决方案

#1


1  

Method 1: Edit PHP Code

Index 0 is not available because you are fetching an associative array from PHP using mysql_fetch_assoc.

索引0不可用,因为您使用mysql_fetch_assoc从PHP获取关联数组。

Using mysql_fetch_array will return an array containing both zero-based indices and associative keys by default.

默认情况下,使用mysql_fetch_array将返回一个包含从零开始的索引和关联键的数组。

$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());  
$tasks = array();
while($row = mysql_fetch_array($res)) {
    $tasks[] = array('row'=>$row);
} 
echo json_encode(array('tasks'=>$tasks));

Will output

{"tasks":[{"row":{"0":"1","userid":"1","1":"send email to Bob","task":"send email to Bob","2":"include attached memo","longtask":"include attached memo"}}]}

Now you can retrieve the userid using either of the keys 0 or userid.

现在,您可以使用键0或userid检索用户标识。

Method 2: Edit iOS Code

Edit iOS Code. But for this to work, you will have to know the keys in the row.

编辑iOS代码。但要实现这一点,您必须知道行中的键。

NSString *str = [array objectForKey:@"userid"];`

#1


1  

Method 1: Edit PHP Code

Index 0 is not available because you are fetching an associative array from PHP using mysql_fetch_assoc.

索引0不可用,因为您使用mysql_fetch_assoc从PHP获取关联数组。

Using mysql_fetch_array will return an array containing both zero-based indices and associative keys by default.

默认情况下,使用mysql_fetch_array将返回一个包含从零开始的索引和关联键的数组。

$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());  
$tasks = array();
while($row = mysql_fetch_array($res)) {
    $tasks[] = array('row'=>$row);
} 
echo json_encode(array('tasks'=>$tasks));

Will output

{"tasks":[{"row":{"0":"1","userid":"1","1":"send email to Bob","task":"send email to Bob","2":"include attached memo","longtask":"include attached memo"}}]}

Now you can retrieve the userid using either of the keys 0 or userid.

现在,您可以使用键0或userid检索用户标识。

Method 2: Edit iOS Code

Edit iOS Code. But for this to work, you will have to know the keys in the row.

编辑iOS代码。但要实现这一点,您必须知道行中的键。

NSString *str = [array objectForKey:@"userid"];`