如何解析数组 PHP->JSON->XCODE 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6017575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:02:28  来源:igfitidea点击:

How to parse arrays in array PHP->JSON->XCODE

phpmysqlxcodejson

提问by Objective Coder

I'm writing a simple php/mysql script that gets all "ads" I have in my database and returns them in JSON format. This works great if I'm only returning one row, ten the parser will split each string into an index om the array. But now I have this PHP code:

我正在编写一个简单的 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);

?>

And it returns the following format:

它返回以下格式:

[["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"]]

(CONTAINING TWO SEPARATE ADS)

(包含两个单独的广告)

When xcode gets this array back it puts the entire first ad in one index of the array. How do I to also put each array in an array like the JSON output does?

当 xcode 取回这个数组时,它会将整个第一个广告放在数组的一个索引中。我如何也像 JSON 输出那样将每个数组放入一个数组中?

How do I process/deserialize this data is my core question!

如何处理/反序列化这些数据是我的核心问题!

Thanks!

谢谢!

回答by Robot Woods

I think you just need to have a two layer reference (last line below) unless I'm misunderstanding something:

我认为你只需要有一个两层参考(下面的最后一行),除非我误解了一些东西:

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