开发者

Read 2 JSON Request from iPhone

开发者 https://www.devze.com 2023-04-13 03:25 出处:网络
on the php side i wrote the following code: ...... sendResponse(200, json_encode($result1)); sendResponse(200, json_encode($result2));

on the php side i wrote the following code:

 ......
 sendResponse(200, json_encode($result1));
 sendResponse(200, json_encode($result2));

Now on the Xcode side i Wrote the Following code:

开发者_运维知识库- (void)requestFinished:(ASIHTTPRequest *)request
{    
    if (request.responseStatusCode == 200) {
    NSString *responseString = [request responseString];
    NSDictionary *responseDict = [responseString JSONValue];
    NSArray *carsType = [responseDict allValues];
    if (carsType != Nil) {
        self.carsTypeArray = carsType;
        [carsTypeTableView reloadData];


    }
 }

the problem is if i comment one of the php codes, the obj-c code work 100%. but i need to know how to read the 2 JSON Respond.

Any idea Please.


Just do this:

$result[] = $result1;
$result[] = $result2;
sendResponse(200, json_encode($result));

[responseDict objectAtIndex:0] will be $result1.

[responseDict objectAtIndex:1] will be $result2.


Fundamentally, HTTP requires one response to each request. You can't send two hTTP response messages in a row and expect the client to understand.

The easiest way to send two pieces of JSON in one HTTP response is to wrap them in a JSON array e.g.

[ { "foo" : "bar" }, { "foo" : "baz" }]

I imagine in PHP if you stick result1 and result2 into a PHP array and JSON encode it, you'd get the right JSON string.

On the iPhone, when you send -JSONValue to the response, you'll get a NSArray returned containing your two JSON objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号