如何从iphone post请求获取json数据 [英] how to get json data from iphone post request

查看:99
本文介绍了如何从iphone post请求获取json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON框架开发一个iPhone应用程序,我调用一个PHP脚本来更新本地服务器上的MySQL数据库。使用这些代码:

I am developing an iPhone App using JSON framework, I am calling a PHP script to update MySQL database on local server. Using these code:

NSString *jsonString = [sendData JSONRepresentation];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString*post = [NSString stringWithFormat:@"&json=%@", jsonString];
NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

[request setURL:[NSURL URLWithString:@"http://localhost:8888/update.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postData];

我想问:

什么是PHP脚本从该请求中获取数据,以及如何解码JSON数据到PHP对象以更新到数据库?

What is the PHP script to get data from that request, and how I can decode JSON data to PHP object to update to database?

任何帮助将不胜感激。 p>

Any help would be appreciated.

推荐答案

您将在变量$ _POST ['json']中找到您的数据,您可以查看通过POST使用:

You will find your data in the variable $_POST['json'], you can take a look at what you have received via POST by using:

<?php print_r($_POST); ?>

一旦你确定了你的数据在哪里,你可以从JSON传递到PHP数据使用:

Once you have identified where is your data, you can pass from JSON to PHP data using:

<?php $phpObj = json_decode($_POST['json']); ?>

同样,您可以使用print_r来查看数据的结构:

Again, you can use print_r to look at the structure of your data:

<?php print_r($phpObj); ?>

这篇关于如何从iphone post请求获取json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆