【问题标题】:Parsing JSON from PHP by SBJsonSBJson 从 PHP 解析 JSON
【发布时间】:2012-06-05 09:52:26
【问题描述】:

现在我正在尝试通过 PHP 将 jpeg 文件发布到 MySQL 并从 MySQL 获取到 iOS。

在 GET 方法中,我使用 json_encode() 在 PHP 脚本上将 jpeg(作为 NSData)和一些相关数据(例如标题、时间戳)编码为 JSON。

    {"caption":"(captiondata)","img":"(imagedata)","timestamp":"(timestampdata)"}

然后我将数据设置为数组,例如,

    [{"caption":"(captiondata)","img":"(imagedata)","timestamp":"(timestampdata)"},
     {"caption":"(captiondata)","img":"(imagedata)","timestamp":"(timestampdata)"},
     .......,
     {"caption":"(captiondata)","img":"(imagedata)","timestamp":"(timestampdata)"}]

我相信我可以通过 PHP 上的 echo (json_encode ()) 和 iOS 上的 SBJsonParser 解析并获取此 JSON,但我的应用程序没有任何返回。这是我在 iOS 中的代码。(我也使用 Three20 的 TTURLRequest)

    TTURLRequest* request = [TTURLRequest requestWithURL:url delegate:self];
    request.cachePolicy = cachePolicy;
    TTURLJSONResponse* response = [[[TTURLJSONResponse alloc] init] autorelease];
    request.response = response;
    [request send];

    - (void)requestDidFinishLoad:(TTURLRequest*)request {
        TTURLJSONResponse* response = request.response;
        NSLog(@"%@",response);
    }

【问题讨论】:

  • 如果您的目标是 iOS5+,请考虑使用 Apple 自己的 JSON 解析器。 developer.apple.com/library/ios/#documentation/Foundation/…
  • @Devraj 似乎太好了。谢谢你的回答。
  • 控制台的输出是什么?尝试输入NSLog(@"didFinishLoad fired"); 来判断requestDidFinishLoad 是否被解雇。

标签: php objective-c json three20 sbjson


【解决方案1】:

可以打印requestDidFinishLoad:中的所有日志吗?

响应是TTURLJSONResponserootObject

- (void)requestDidFinishLoad:(TTURLRequest*)request {

    TTURLJSONResponse *response = request.response;
    NSDictionary *dict = response.rootObject;
    NSLog(@"dict : %@",dict);

}

在你的情况下,

- (void)requestDidFinishLoad:(TTURLRequest*)request {
    TTURLJSONResponse* response = request.response;
    NSLog(@"%@",response);
}

响应可能类似于<TTURLJSONResponse: 0x125398c0>

如果仍然没有返回,您可以检查 requestDidFinishLoad: 是否被调用,而不是缓存问题。

【讨论】:

  • 感谢您的回答。我更改了代码并进行了检查,但 PHP 只返回 ()。我现在设置 request.cachePolicy = TTURLRequestCachePolicyNone。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多