![](/img/trans.png)
[英]HTML(input)->php->mysql->php->json->HTML render
[英]How to parse arrays in array PHP->JSON->XCODE
我正在编写一个简单的php / mysql脚本,该脚本获取数据库中所有的“广告”并以JSON格式返回它们。 如果我只返回一行,那么十个解析器会将每个字符串拆分为数组的索引,这将非常有用。 但是现在我有了这个PHP代码:
<?php
// Database credentials
$host = 'localhost';
$db = 'project';
$uid = 'root';
$pwd = '';
$SSN = $_GET['ssn'];
// Connect to the database server
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");
//select the json database
mysql_select_db($db) or die("Could not select database");
// Create an array to hold our results
$arr = array();
//Execute the query
$rs = mysql_query("SELECT * FROM all_ads WHERE SSN = $SSN ORDER BY datePosted");
// Add the rows to the array
while($obj = mysql_fetch_row($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>
它返回以下格式:
[["4","Hyr ut i natt","321654987","couch_surfing_ads","2011-05-16 13:49:58"],["5","Maybe not","456893214","vacation_resident_ads","2011-05-16 14:22:34"]]
(包含两个单独的广告)
当xcode取回该数组时,它将整个第一个广告放入该数组的一个索引中。 如何将每个数组也放入JSON输出一样的数组中?
我的核心问题是如何处理/反序列化此数据!
谢谢!
我认为您只需要一个两层引用(下面的最后一行),除非我误会了一些东西:
NSString *response = [[NSString alloc] initWithContentsOfURL:yourSourceUrl];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSArray *ads = [responseString JSONValue];
NSLog(@"%@",[[ads objectAtIndex:0] objectAtIndex:1]); //Hyr ut i natt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.