【问题标题】:IOS: POST Array values to JSON ServerIOS:将数组值发布到 JSON 服务器
【发布时间】:2017-06-20 07:49:06
【问题描述】:

我有一个这种形式的数组:

(
        {
        itemId = 18;
        itemPrice = "90.00";
        itemQuantity = 100;
    },
        {
        itemId = 17;
        itemPrice = "88.00";
        itemQuantity = 120;
    }
)

我必须以这种形式将其发布到 Json 服务器

im[18][price]=90
im[18][stock]=100
im[18][check]=1

im[17][price]=88
im[17][stock]=120
im[17][check]=1

请帮助我如何做到这一点,目前我正在使用以下代码

for (NSInteger i = 0; i < selectedIdArray.count; i++)
    {
        mapData = [NSString stringWithFormat:@"api_key=bf45c093e542f057c123ae7d6&menu_name=%@&im[%@][price]=%@&im[%@][stock]=%@&im[%@][check]=%@",MenuNameStr,selectedIdArraySTR,selectedPriceArraySTR,selectedIdArraySTR,selectedQuantityArraySTR,selectedIdArraySTR,@"1"];
    }

它给出以下响应

map data api_key=bf45c093e542f057c123ae7d6&menu_name=&im[18,17][price]=90.00,88.00&im[18,17][stock]=100,120&im[18,17][check]=1

请有人帮我将以下数据分别发送到服务器

【问题讨论】:

  • 您的问题到底是什么?你做一个POST?那你不把那个数据放到 HTTPBody 里吗?
  • 同样的事情,我不知道如何破坏阵列。请帮我为单独的参数发送单独的数据。目前它需要完整的参数数组
  • 您显示的第二种格式不是 JSON。
  • 请帮我解决这个问题我如何发布这个数组
  • 你能告诉我你想如何发布数据吗?实际的 url 或请求是什么?

标签: ios objective-c arrays json dictionary


【解决方案1】:

假设这是你的字典。

{
    itemId = 17;
    itemPrice = "88.00";
    itemQuantity = 120;
}

先获取dict中元素的个数。

int getKeysCount = [allKeys count];

假设这是您需要的输出

im[18][price]=90
im[18][stock]=100
im[18][check]=1

im[17][price]=88
im[17][stock]=120
im[17][check]=1

你需要更新你的循环

mapData = [NSStringstringWithFormat:@"api_key=XX--XX&menu_name=%@"];

for (int i = 0; i < selectedIdArray.count; i++) {
    int k = itemId[i];

    for (int j = 0; j < getKeysCount.count; i++) {
        mapData = [NSString stringWithFormat:@"&im[k][price]=%@&im[k][stock]=%@&im[k][check]=%@",itemPrice[i],itemQuantity[i],@"1"];
    }
}
NSLog(@"%@",mapData);

//Let me know if you find any query in this answer

【讨论】:

  • 这里的 itemId[i] 是数组还是字符串?
  • 我将 itemId = 17 分配为 int k = itemId[i];
  • 但我有 2 个商品 ID
  • 将其放入一个循环中,它会自动更新,这就是我放置 itemId[i] 的原因。
  • @Abhi 我的回答解决了你的问题吗?
【解决方案2】:

我想你想从文件中解析 JSON 这里 Swift JSON 解析 How to parse a JSON file in swift?Objective-C 解析 How do I parse JSON with Objective-C?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多