![](/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.