簡體   English   中英

如何將NSStrings的NSArray轉換為Json String iOS

[英]How to convert NSArray of NSStrings into Json String iOS

我有一個滾動數組

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];

我需要在Web服務請求中發送此數組

其格式如下(采用JSON格式)

JSON數據

{
  "existingRoll":["22","34","45","56"], // Array of roll numbers
  "deletedRoll":["20","34","44","56"] // Array of roll numbers
}

但我在將數字滾動數字(rollArray)轉換為所需格式的json String時遇到問題。

我正在嘗試這個

 NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
  [postDict setValue:[rollArray componentsJoinedByString:@","] forKey:@"existingRoll"];

 NSString *str = [Self convertToJSONString:postDict]; // converts to json string

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str options:0 error:nil];

 [request setHTTPBody:jsonData];

我使用的是iOS 7

無需使用以下代碼段:

  1. [rollArray componentsJoinedByString:@","]
  2. NSString *str = [Self convertToJSONString:postDict];

您可以使用以下代碼創建JSON:

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];
NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
[postDict setValue:rollArray forKey:@"existingRoll"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];

// Checking the format
NSLog(@"%@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

嘗試這個 :

NSDictionary *object = @{
    @"existingRoll":@[@"22",@"34",@"45",@"56"],
    @"deletedRoll":@[@"20",@"34",@"44",@"56"]
};

if ([NSJSONSerialization isValidJSONObject:object]) {
    NSData* data = [ NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil ];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(str);
}
NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init];

    [postDict setValue:@"Login" forKey:@"methodName"];
    [postDict setValue:@"admin" forKey:@"username"];
    [postDict setValue:@"12345" forKey:@"password"];
    [postDict setValue:@"mobile" forKey:@"clientType"];


    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];

    // Checking the format
    NSString *urlString =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


    // Convert your data and set your request's HTTPBody property
    NSString *stringData = [[NSString alloc] initWithFormat:@"jsonRequest=%@", urlString];

    //@"jsonRequest={\"methodName\":\"Login\",\"username\":\"admin\",\"password\":\"12345\",\"clientType\":\"web\"}";

    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];

您可以使用以下方法從任何類型的NSArray獲取Json字符串:

NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:rollArray options:0 error:nil];

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"Json string is: %@", jsonString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM