How to pass an array in an url query parameter using nsurl in objective-c?

不打扰是莪最后的温柔 提交于 2019-12-12 02:51:59

问题


The API need to pass an array in an url query parameter, how to acheive this in iOS?

I only know how to pass a single vaue, like the API below : ?title=Design Milk&id=feed/http://feeds.feedburner.com/design-milk

API sample:

  "title": "Design Milk",
  "id": "feed/http://feeds.feedburner.com/design-milk",
  "categories": [
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/design",
      "label": "design"
    },
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/weekly",
      "label": "weekly"
    },
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/global.must",
      "label": "must reads"
    }
  ]

回答1:


Create a collection and then use NSJSONSerialization to create JSON data representation. Use the resulting data as the POST data.

NSDictionary *parameters = @{
  @"title": @"Design Milk",
  @"id": @"feed/http://feeds.feedburner.com/design-milk",
  @"categories": @[
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/design",
              @"label": @"design"
              },
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/weekly",
              @"label": @"weekly"
              },
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/global.must",
              @"label": @"must reads"
              }
          ]
};

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictData options:0 error:&error];


来源:https://stackoverflow.com/questions/24162871/how-to-pass-an-array-in-an-url-query-parameter-using-nsurl-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!