【问题标题】:Writing out JSON data to a simple textfile将 JSON 数据写入一个简单的文本文件
【发布时间】:2011-04-20 18:52:05
【问题描述】:

与往常一样,我搜索了论坛并用谷歌搜索了自己有点疯狂,却无法弄清楚我做错了什么。因此,我求助于经常光顾这个网站的伟大人物,希望能找到答案。 我正在构建一个将与数据库通信的应用程序,在此过程中,我正在尝试学习如何使用 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:

在我看来,数据是存在的,并且是格式化的,但由于某种原因没有被发送或接收。这里希望代码中的某个地方只是一些愚蠢的错误,因为我已经盯着这个两天了。

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

【问题讨论】:

    标签: php iphone json post


    【解决方案1】:

    fwrite 只能写入字符串,而 var_dump 不返回字符串。 而且...,json_decode 不起作用,因为您的发布请求不是有效的 JSON。

    所以,我认为这对你有用:

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

    【讨论】:

    • 感谢您的回复。我尝试了代码,但它只在文本文件中写出 []。您提到我的发布请求不是有效的 JSON。这是为什么?我错过了什么?
    • 不..,如果您想使用 json_decode(在您的示例中),POST 请求也必须采用 JSON 格式(..到目前为止,我可以阅读 XCode,但不是)。如果您将 $_POST 变量更改为 $_GET 变量并调用 localhost:8888/iWish/json_post.php?var=test&var2=example,会发生什么情况?您在测试文件中看到有效的 JSON 字符串了吗?
    • 如果我将 $_POST 更改为 $_GET 并调用 localhost:8888/iWish/json_post.php?var=test&var2=example 那么被写出的测试文件如下所示: {"var":" test","var2":"example"}.
    • 所以看起来 PHP 脚本工作正常,"{"var":"test","var2":"example"}" 是一个有效的 JSON 字符串。我认为您在 XCode 中的 POST 请求存在一些问题。但是很抱歉,我无法在 XCode 方面为您提供帮助。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多