简体   繁体   English

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

[英]PHP/IOS: what is best way to encode json for web service?

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. 用sql查询创建记录集。 Each record in the recordset is a row. 记录集中的每条记录都是一行。

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 当我尝试,我得到一个错误,它无法弧度排在指数:0

      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. 在此先感谢您的任何建议。

Method 1: Edit PHP Code 方法1:编辑PHP代码

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 . 现在,您可以使用键0userid检索用户userid

Method 2: Edit iOS Code 方法2:编辑iOS代码

Edit iOS Code. 编辑iOS代码。 But for this to work, you will have to know the keys in the row. 但要实现这一点,您必须知道行中的键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM