将JSON数据写入简单文本文件

5

一如既往,我在论坛上搜索并自己谷歌了一番,但仍然无法弄清楚我做错了什么。因此,我转向在这个网站上经常出现的伟大思想,希望能找到答案。 我正在构建一个应用程序,将与数据库通信,并尝试学习如何使用JSON通过iPhone检索和发布数据到数据库,使用在网上找到的各种示例。我已成功使用JSON从Web检索数据并在tableview中显示它,但是当我尝试POST数据时,似乎什么都不起作用。 基本上,我有一个简单的php脚本,应该将接收到的数据写入文本文件(见下文)。

<?php
//header('Content-type: application/x-json');

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = var_dump($_POST);
fwrite($fh, $stringData);

$stringData = "=== JSON Decoded ===";  
fwrite($fh, $stringData);

$stringData = $_POST["tmp"];
fwrite($fh, json_decode($stringData));

$stringData = "=== JSON Decoded ===";
fwrite($fh, $stringData);

fclose($fh);
?>

问题在于该脚本似乎没有接收到任何内容。当向其提交数据时,它会创建一个看起来像这样的文件。因此它确实创建了文件,但里面什么都没有。
=== JSON Decoded ====== JSON Decoded ===

以下是我在XCode中的POST方法代码。
-(IBAction)poststuff:sender{

    NSString *stuffToPost = [[NSString alloc] initWithFormat:@"Work, damn you!"];

    NSURL *jsonURL = [NSURL URLWithString:@"http://localhost:8888/iWish/json_post.php"];

    NSData *postData = [stuffToPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSLog(@"Stuff I want to POST:%@", stuffToPost);
    NSLog(@"Data I want to POST:%@", postData);

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:jsonURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;

    NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *data = [[NSString alloc] initWithData:serverReply encoding:NSUTF8StringEncoding];
    NSLog(@"Raw Data:", data);
}

当触发该方法并写出空文本文件时,控制台将如下所示:
2010-10-04 14:10:16.666 iWish[38743:207] Stuff I want to POST:Work, damn you!
2010-10-04 14:10:16.668 iWish[38743:207] Data I want to POST:<576f726b 2c206461 6d6e2079 6f7521>
2010-10-04 14:10:16.673 iWish[38743:207] serverReply:

我觉得数据已经存在,格式也没问题,但出于某种原因没有被发送或接收。希望只是代码中某个愚蠢的错误,因为我已经盯着它看了两天。

如果有任何帮助,我将不胜感激。谢谢!

1个回答

3

使用 fwrite 函数只能写入字符串,而 var_dump 不会返回字符串。另外,json_decode 也无法使用,因为您的 POST 请求不是有效的 JSON 格式。

所以,我认为下面的代码可以解决您的问题:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = json_encode($_POST);
fwrite($fh, $stringData);
fclose($fh);

谢谢回复。我尝试了这段代码,但它只在文本文件中写出[]。你提到我的POST请求不是有效的JSON格式。为什么会这样?我错过了什么吗? - Glitch
不,如果你想在你的例子中使用json_decode,POST请求也必须是JSON格式(就我所看到的XCode而言)。如果你将$_POST变量更改为$_GET变量,并调用http://localhost:8888/iWish/json_post.php?var=test&var2=example会发生什么?你能在测试文件中看到有效的JSON字符串吗? - Bas van Dorst
如果我将 $_POST 更改为 $_GET 并调用 localhost:8888/iWish/json_post.php?var=test&var2=example,那么写出的测试文件看起来像这样:{"var":"test","var2":"example"}。 - Glitch
看起来 PHP 脚本工作正常,"{"var":"test","var2":"example"}" 是一个有效的 JSON 字符串。我认为 XCode 中的 POST 请求有一点问题。但很抱歉,我无法帮助你解决 XCode 的问题。 - Bas van Dorst

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接