-4

在这个应用程序中,有人为我构建了这个代码来解析一个 json 数组:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/lending.php?category_id=1",host_name ]]];
[request setRequestMethod:@"GET"];

它工作正常,现在,我需要的是插入这个数组而不是lendig.php,这实际上是json数组的输出:

[{"name":"Ekspres Kredi","category_id":"1","id":"2"},{"name":"Kredi p\u00ebr Pushime ","category_id":"1","id":"4"},{"name":" Kredi Konsumuese ","category_id":"1","id":"5"},{"name":"Auto Kredi ","category_id":"1","id":"6"}]

我怎样才能插入呢?

它应该是这样的:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"[{"name":"Ekspres Kredi","category_id":"1","id":"2"},{"name":"Kredi p\u00ebr Pushime ","category_id":"1","id":"4"},{"name":" Kredi Konsumuese ","category_id":"1","id":"5"},{"name":"Auto Kredi ","category_id":"1","id":"6"}]",host_name ]]];
[request setRequestMethod:@"GET"];
4

1 回答 1

0

首先你应该清楚你想做什么。无论如何,我假设您想将一些数据发送到服务器,在这种情况下您使用 POST 请求。

NSString *jsonString = @"{\"name\":\"Ekspres Kredi\",\"category_id\":\"1\",\"id\":\"2\"},{\"name\":\"Kredi p\u00ebr Pushime \",\"category_id\":\"1\",\"id\":\"4\"},{\"name\":\" Kredi Konsumuese \",\"category_id\":\"1\",\"id\":\"5\"},{\"name\":\"Auto Kredi \",\"category_id\":\"1\",\"id\":\"6\"}"


NSURL *url = [NSURL URLWithString:@"http://myserver.com/post.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[jsonString  dataUsingEncoding:NSUTF8StringEncoding]]; 
[request startAsynchronous];

通过设置请求的主体,使用的方法将是 POST。

于 2013-01-25T20:55:14.100 回答