dongyi2006 2011-12-26 18:07
浏览 77
已采纳

JSON解析:来自PHP5(攻击数据库:mySQL)ios5 iphone

I hope someone can help me put because I've trying the whole day :( ( newly stuff )

I have a database mySQL, which contains one table "products" ( two rows : "id" and "stock" ). And I want my ios5 app to send an "id" and receive the "stock" of that product.

In my PHP code:

echo json_encode(array(
     'id'=>$id,
     'stock'=>$stock, ));

Which I believe sends a JSON to my app, my app receives this JSON in a function called:

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    NSString *responseStringWEB = [request responseString];
    NSLog(@"STRING:: %@ 
", responseStringWEB); //3
    NSDictionary *responseDict = [responseStringWEB JSONValue];
    NSLog(@"DICT: %@ 
", responseDict); //3
    NSString *id_producto = [responseDict objectForKey:@"id"];
    NSString *stock = [responseDict objectForKey:@"stock"];
    NSLog(@"ID: %@
", id_producto); //3
    NSLog(@"stock: %@", stock); //3
}

and checking the console I get:

**`STRING`::**
Connection establishedDatabase selected..
***{"id":"3","stock":"46"}***
Connection closedDesconectado
2011-12-26 18:58:57.170 CaeDeCajon[1998:16403] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x984aeb0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
2011-12-26 18:58:57.171 CaeDeCajon[1998:16403] **`DICT`**: (null)
2011-12-26 18:58:57.171 CaeDeCajon[1998:16403] **`ID`**: (null)
2011-12-26 18:58:57.171 CaeDeCajon[1998:16403] **`stock`**: (null)

The question is : I do not know what format the JSON is coming ( array, How should I parse the NSstring responseStringWEB to get those two values ( ID and STOCK ). It seems I receive them from the database but I do not reach to extract them.

HELP :) thank you ,

EDITING::

Thanks. It really Helped.

It seemed that there has had something to do with the multiple echos I used in the PHP code. Now I only have one echo, sending data in json format. It works perfectly with my database and my app: I receive the whole table ( "id" and "stock" ) of all items. Thanks.

But I have found another obstacle ( no wonder ), is that I need to change the database once the products have been sold, and as they´re not usually sold 1 by 1 must post arrays into PHP,, my intention is to POST the id and reductor(reductor represent how many products of that "id" were sold ) of the products/items affected ( array_id and array_reductor).

IOS5 CODE:

NSArray *array_id=[[NSArray alloc]initWithObjects:@"0",@"3",@"5", nil]; 

//with the id products;

NSArray *array_reductor=[[NSArray alloc]initWithObjects:@"10",@"5",@"40", nil];

//with the number of products sold ( so I have to decrease the previous stock number in the database by these to obtain the current stock numbers ).

NSURL *url=[[NSURL alloc]initWithString:@"http://www.haveyourapp.com/promos/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:array_id forKey:@"id"];
[request setPostValue:array_reductor forKey:@"reductor"];

[request setDelegate:self];
[request startAsynchronous];

MY PHP FILE:

if (isset($_POST['id'])&&isset($_POST['reductor'])) // check if data is coming {

$id = array();          // I create arrays
$reductor = array();

$id=$_POST['id'];             // and store arrays in them ( At least is what I believe )
$reductor=$_POST['reductor'];

$connection = new createConnection(); //i created a new object
$connection->connectToDatabase(); // connected to the database
$connection->selectDatabase();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// Stock reduction in the items affected////////////////////////////////


$num=mysql_numrows($id);
$i=0;
$stock_anterior=array();
while ($i < $num) {

$query=" SELECT  stock FROM productos WHERE id = $id[$i]";
$stock_anterior[$i] = mysql_query($query);
++$i;
}


$l=0;

$num2=mysql_numrows($id);

while ($l < $num2) {

$stock_reductor[$l] = $stock_anterior[$l] - $reductor[$l];

$query = "UPDATE productos SET stock = '$stock_reductor[$l]' WHERE id = $id[$l] ";
mysql_query($query);


++$l;

}



$connection->closeConnection();

But my code is not working, I don not know if the problem is in my app or in the PHP file ( likely ), but how can I receive those two arrays and work with them????

Thanks in advance

I spend a lot of time on stack Overflow: VERY USEFULLLLLLLL!!!!!

  • 写回答

3条回答 默认 最新

  • duangu6431 2011-12-26 23:56
    关注

    Check the PHP script output using a tool such as HTTPScoop. I suspect that something is wrong, based on the console output, which contains the lines Connection establishedDatabase selected.. and Connection closedDesconectado...

    **`STRING`::**
    Connection establishedDatabase selected..
    ***{"id":"3","stock":"46"}***
    Connection closedDesconectado
    

    It looks like you've got some logging from that script that is printed before the JSON starts, which isn't accepted by the JSON parser on the iOS end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?