如何使用JSON将iPhone应用程序中的对象数组发送到PHP脚本

时间:2022-05-08 20:50:41

I have an iPhone app with an array of objects that I want to send to a PHP script and have them be stored in a mySQL database. The objects in the array contain only floating points and strings, nothing special.

我有一个带有一系列对象的iPhone应用程序,我想将它们发送到PHP脚本并将它们存储在mySQL数据库中。数组中的对象只包含浮点和字符串,没什么特别的。

From what I understand the best way to send the array to the php script is to convert the array into an JSON, send it to the php script via http post, and do a json_decode on the other end. However I'm having a hard time figuring out how to convert the array into a json object.

根据我的理解,将数组发送到php脚本的最佳方法是将数组转换为JSON,通过http post将其发送到php脚本,并在另一端执行json_decode。但是我很难搞清楚如何将数组转换为json对象。

Can someone give me a pointer of where to start?

有人可以给我一个指示从哪里开始?

3 个解决方案

#1


3  

Start with the JSON library for objective-c:

从objective-c的JSON库开始:

http://code.google.com/p/json-framework

That will make the serialization much easier, as it has a method to convert an NSArray to JSON.

这将使序列化更容易,因为它有一个方法将NSArray转换为JSON。

http://json-framework.googlecode.com/svn/trunk/documentation/interfaceSBJSON.html#830175bff0fbef8ccb82da852a154b48

From there you can post using different mechanisms, but NSURLConnection is the easiest. You can do synch or asynch, depending on your needs.

从那里你可以使用不同的机制发布,但NSURLConnection是最简单的。您可以根据需要进行同步或异步。

You will need to set some headers on the request for json:

您需要在json请求上设置一些标头:

NSMutableURLRequest * r = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[r addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[r setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];    

#2


4  

http://code.google.com/p/touchcode/wiki/TouchJSON

"TouchJSON is parser and generator for JSON implemented in Objective C.

“TouchJSON是在Objective C中实现的JSON的解析器和生成器。

It is based on my CocoaJSON code: http://toxicsoftware.com/cocoajson/

它基于我的CocoaJSON代码:http://toxicsoftware.com/cocoajson/

Here is how to use it: TouchJSONHowTo"

以下是如何使用它:TouchJSONHowTo“

#3


0  

JSON is a simple text format. You can write the string out yourself by looping over your array if you don't want to use a library.

JSON是一种简单的文本格式。如果您不想使用库,可以通过循环遍历数组来自行编写字符串。

http://www.json.org/example.html

#1


3  

Start with the JSON library for objective-c:

从objective-c的JSON库开始:

http://code.google.com/p/json-framework

That will make the serialization much easier, as it has a method to convert an NSArray to JSON.

这将使序列化更容易,因为它有一个方法将NSArray转换为JSON。

http://json-framework.googlecode.com/svn/trunk/documentation/interfaceSBJSON.html#830175bff0fbef8ccb82da852a154b48

From there you can post using different mechanisms, but NSURLConnection is the easiest. You can do synch or asynch, depending on your needs.

从那里你可以使用不同的机制发布,但NSURLConnection是最简单的。您可以根据需要进行同步或异步。

You will need to set some headers on the request for json:

您需要在json请求上设置一些标头:

NSMutableURLRequest * r = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[r addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[r setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];    

#2


4  

http://code.google.com/p/touchcode/wiki/TouchJSON

"TouchJSON is parser and generator for JSON implemented in Objective C.

“TouchJSON是在Objective C中实现的JSON的解析器和生成器。

It is based on my CocoaJSON code: http://toxicsoftware.com/cocoajson/

它基于我的CocoaJSON代码:http://toxicsoftware.com/cocoajson/

Here is how to use it: TouchJSONHowTo"

以下是如何使用它:TouchJSONHowTo“

#3


0  

JSON is a simple text format. You can write the string out yourself by looping over your array if you don't want to use a library.

JSON是一种简单的文本格式。如果您不想使用库,可以通过循环遍历数组来自行编写字符串。

http://www.json.org/example.html