如何用逗号分隔的字符串创建字典数组?

时间:2014-09-30 16:28:59

标签: ios objective-c nsstring nsarray nsdictionary

我正在接收逗号分隔的字符串,如下所示,它将被转换为字典数组。我尝试使用[myString componentsSeparatedByString:@","];然而它提供了完全不同的输出。转换它的正确方法是什么?

  NSString *cancellationStr ==> [{"cutoffTime":"0-2","refundInPercentage":"0"},{"cutoffTime":"2-3","refundInPercentage":"50"},{"cutoffTime":"3-24","refundInPercentage":"90"}]

 NSArray *array = [cancellationStr componentsSeparatedByString:@","]; 

  //Gives response like below

(
"[{\"cutoffTime\":\"0-2\"",
"\"refundInPercentage\":\"0\"}",
"{\"cutoffTime\":\"2-3\"",
"\"refundInPercentage\":\"50\"}",
"{\"cutoffTime\":\"3-24\"",
"\"refundInPercentage\":\"90\"}]"
)

使用单例HTTP类获取和解析的代码

   NSString *urlString=[NSString stringWithFormat:@"%@%@sourceCity=%@&destinationCity=%@&doj=%@",BASE_URL,AVAILABLE_BUSES,source,destination,dateStr];
   urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   NSURL *url= [NSURL URLWithString:urlString];

    SuccessBlock successBlock = ^(NSData *responseData){
    NSError *error;
    jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
     bArray = [jsonDictionary objectForKey:@"apiAvailableBuses"];
      };

     FailureBlock failureBlock = ^(NSError *error){
     NSLog(@"%@",error);
     };


HTTPRequest *request = [[HTTPRequest alloc]initWithURL:url successBlock:successBlock failureBlock:failureBlock];
[request startRequest];  

上面的成功块收到了我想要转换的字符串。

    NSString *cancellationStr = [[bArray objectAtIndex:rowIndex]valueForKey:@"cancellationPolicy"];

    bObject.cancellationPolicy = [cancellationStr componentsSeparatedByString:@","];
    NSLog(@"Cancellation Array : %@",bObject.cancellationPolicy);

1 个答案:

答案 0 :(得分:0)

所以," apiAvailableBusses"是一个像这样的元素数组:

{
    "operatorId":196,
    "operatorName":"Sahara Bus",
    "departureTime":"6:45 PM",
    "mTicketAllowed":false,
    "idProofRequired":false,
    "serviceId":"1602",
    "fare":"450",
    "busType":"Sahara Hitech Non A/c",
    "routeScheduleId":"1602",
    "availableSeats":29,
    "partialCancellationAllowed":false,
    "arrivalTime":"07:30 AM",
    "cancellationPolicy":"[{\"cutoffTime\":\"12\",\"refundInPercentage\":\"0\"},{\"cutoffTime\":\"24\",\"refundInPercentage\":\"50\"}]",
    "commPCT":0.0,
    "boardingPoints":[
        {
            "time":"09:05PM",
            "location":"Ameerpet,Kaveri Kmakshi Travels,Beside RKS Mall,Ameerpet, Mr.Aman-9391830030.",
            "id":"6"
        },
        {
            "time":"09:15PM",
            "location":"Punjagutta,Sai Ganesh Travels, Punjagutta, Mr.Aman-9391830030.",
            "id":"2241"
        },
        {
            "time":"09:30PM",
            "location":"Lakdi-ka-pool,Sahara Travels,Hotel Sri Krishna Complex,Mr.Aman-9391830030.",
            "id":"2242"
        },
        {
            "time":"08:45PM",
            "location":"ESI,Behind Bajaj Show Room, Near Nani Travels. Mr.Aman-9391830030.",
            "id":"2287"
        },
        {
            "time":"09:45PM",
            "location":"Nampally,Near Khaja Travels, Nampally,Mr.Aman-9391830030.",
            "id":"2321"
        },
        {
            "time":"09:16PM",
            "location":"Paradise,Near SVR Travels, Paradise,Mr.Aman-9391830030.",
            "id":"2322"
        },
        {
            "time":"10:00PM",
            "location":"Afzalgunj,Sahar Travels,Mr.Aman-9391830030.",
            "id":"2323"
        },
        {
            "time":"09:00PM",
            "location":"Secunderbad Station,Near Asian Travels.Mr.Aman-9391830030.",
            "id":"2336"
        }
    ],
    "droppingPoints":null,
    "inventoryType":0
}

" cancellationPolicy" element是一个字符串,它被称为"嵌入式JSON"。 (为什么他们在这种情况下这样做我还没有线索。)这意味着字符串必须再次通过NSJSONSerialization 提供。要将NSString转换为NSData(再次通过NSJSONSerialization),您需要先使用dataWithEncoding:NSUTF8StringEncoding