PHP/IOS:为Web服务编码json的最佳方法是什么? [英] PHP/IOS: what is best way to encode json for web service?

查看:89
本文介绍了PHP/IOS:为Web服务编码json的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I have a standard mysql/php setup.

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

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.

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

该Web服务产生以下输出:

The web service produces the following output:

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

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

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.

结构与本教程和其他我将json读入IOS(均不使用php/mysql)时发现的其他源中的结构不同.

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中读取此json来获取用户ID,任务和其他变量.

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.

尝试此操作时,出现错误,提示它不能在索引:0处排行

When I try this, I get an error that it cannot rad row at index: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.

推荐答案

方法1:编辑PHP代码

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

Method 1: Edit PHP Code

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

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

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

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

将输出

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

现在,您可以使用0userid键之一来检索用户ID.

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

编辑iOS代码.但这要起作用,您将必须知道行中的键.

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

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

这篇关于PHP/IOS:为Web服务编码json的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆