期望Json输出格式如[{},{}]

时间:2021-06-28 14:05:49

I'm using Json to retrieve the info by webServices. I got a file on PHP to achieve it but the O/P format is different .

我使用Json来通过webServices检索信息。我有一个关于PHP的文件来实现它,但是O/P格式不同。

$sql=mysql_query("select phone1 from xxx where id='".$amp."'");

$response = array();
while($row=mysql_fetch_array($sql))
{
//    print_r($row);
    $sql_query=mysql_query("select x,y,z from tblRepairQueue where phoneNo='".$row['phone1']."'");
    while($row1=mysql_fetch_array($sql_query)){

        $ackNo=$row1['ackNo'];
        $repairStatus=$row1['repairStatus'];
        $savedAt=$row1['savedAt'];

        $response[]=array("ackNo"=>"$ackNo","repairStatus"=>"$repairStatus","savedAt"=>"$savedAt");
    }}
print json_encode($response);

Actually I'm using this on IOS to decode using

实际上我在IOS上用这个来解码

    NSMutableDictionary *userDetails = [NSJSONSerialization JSONObjectWithData:returnData
   options:0 error:nil];

The O/p which I'm getting for

我要的O/p

NSLog(@"%@", userDetails);

NSLog(@ % @”,userDetails);

(
        {
        ackNo = "21031221201377 ";
        repairStatus = "Closed and Complete";
        savedAt = "0000-00-00 00:00:00";
    }
)

I'm expecting is like [{}, {}]

我期望的是[{},{}]

 [
            {
            ackNo = "21031221201377 ";
            repairStatus = "Closed and Complete";
            savedAt = "0000-00-00 00:00:00";
        }
    ]

How to achieve it , I don't know much about PHP . So grateful if anyone's of suggestion .

如何实现它,我对PHP知之甚少。如果有人建议的话,我很感激。

2 个解决方案

#1


1  

In iOS () refers to Arrays and {} refers to dictionary. So you are getting the correct response.

在iOS()中表示数组,{}表示字典。所以你得到了正确的反应。

You need to change your code like:

你需要改变你的代码:

NSMutableArray *userDetails = [NSJSONSerialization JSONObjectWithData:returnData
   options:0 error:nil];

And access the data like:

并访问数据,如:

NSMutableDictionary *user = [userDetails objectAtIndex:0];
NSLog(@"ackNo : %@",[user objectForKey:@"ackNo"]);

#2


0  

You are getting the correct response. The only thing I would change is remove the quotes in your variables when you are creating the response array.

你得到了正确的回答。我唯一要做的就是在创建响应数组时删除变量中的引号。

$response[]=array("ackNo"=>$ackNo,"repairStatus"=>$repairStatus,"savedAt"=>$savedAt);

Its a personal choice, but I think is more readable that way.

这是个人的选择,但我认为这样更容易读懂。

#1


1  

In iOS () refers to Arrays and {} refers to dictionary. So you are getting the correct response.

在iOS()中表示数组,{}表示字典。所以你得到了正确的反应。

You need to change your code like:

你需要改变你的代码:

NSMutableArray *userDetails = [NSJSONSerialization JSONObjectWithData:returnData
   options:0 error:nil];

And access the data like:

并访问数据,如:

NSMutableDictionary *user = [userDetails objectAtIndex:0];
NSLog(@"ackNo : %@",[user objectForKey:@"ackNo"]);

#2


0  

You are getting the correct response. The only thing I would change is remove the quotes in your variables when you are creating the response array.

你得到了正确的回答。我唯一要做的就是在创建响应数组时删除变量中的引号。

$response[]=array("ackNo"=>$ackNo,"repairStatus"=>$repairStatus,"savedAt"=>$savedAt);

Its a personal choice, but I think is more readable that way.

这是个人的选择,但我认为这样更容易读懂。