如何使用for循环优化此代码

时间:2013-08-12 09:46:09

标签: ios objective-c for-loop

有些pne请帮我优化这段代码:

- (NSMutableDictionary *) parseResponse:(NSData *) data {

NSString *myData = [[NSString alloc] initWithData:data
                                         encoding:NSUTF8StringEncoding];
NSLog(@"JSON data = %@", myData);
NSError *jsonParsingError = nil;
//parsing the JSON response
id jsonObject = [NSJSONSerialization
                 JSONObjectWithData:data
                 options:NSJSONReadingAllowFragments
                 error:&jsonParsingError];
if (jsonObject != nil && jsonParsingError == nil)
{
    //NSLog(@"Successfully deserialized...");
        NSLog(@"json object is %@",jsonObject);
    if([jsonObject isKindOfClass:[NSDictionary class]])
    {
        NSLog(@"It has only  dictionary so simply read it");

    }
    else if([jsonObject isKindOfClass:[NSArray class]])
    {
        NSLog(@"It has only  NSArray so simply read it");

    }
    else if([jsonObject isKindOfClass:[NSString class]])
    {
       NSString *stringWithoutOpenCurly = [jsonObject stringByReplacingOccurrencesOfString:@"{" withString:@""];
        NSString *stringWithoutOpenAndCloseCurly = [stringWithoutOpenCurly stringByReplacingOccurrencesOfString:@"}" withString:@""];
        NSArray *arrayOfKeyValue = [stringWithoutOpenAndCloseCurly componentsSeparatedByString:@","];

        NSString *statusString = [arrayOfKeyValue objectAtIndex:0];
        NSArray *statusKeyValueArray = [statusString componentsSeparatedByString:@":"];
        NSString *statusKey, *statusValue;
        statusKey = [[statusKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        statusValue = [[statusKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *messageString = [arrayOfKeyValue objectAtIndex:1];
        NSArray *messageKeyValueArray = [messageString componentsSeparatedByString:@":"];
        NSString *messageKey, *messageValue;
        messageKey = [[messageKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        messageValue = [[messageKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];


        NSString *dataIdString = [arrayOfKeyValue objectAtIndex:2];
        NSArray *dataIdKeyValueArray = [dataIdString componentsSeparatedByString:@":"];
        NSString *dataIdKey, *dataIdValue;
        dataIdKey = [[dataIdKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        dataIdValue = [[dataIdKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *nextString = [arrayOfKeyValue objectAtIndex:3];
        NSArray *nextKeyValueArray = [nextString componentsSeparatedByString:@":"];
        NSString *nextKey, *nextValue;
        nextKey = [[nextKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        nextValue = [[nextKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *b64String = [arrayOfKeyValue objectAtIndex:4];
        NSArray *b64KeyValueArray = [b64String componentsSeparatedByString:@":"];
        NSString *b64Key, *b64Value;
        b64Key = [[b64KeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        b64Value = [[b64KeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];


        NSString *fileNameString = [arrayOfKeyValue objectAtIndex:5];
        NSArray *fileNameKeyValueArray = [fileNameString componentsSeparatedByString:@":"];
        NSString *fileNameKey, *fileNameValue;
        fileNameKey = [[fileNameKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        fileNameValue = [[fileNameKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSMutableDictionary *responseDictionary = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:statusValue, messageValue, dataIdValue, nextValue, b64Value, fileNameValue, nil] forKeys:[NSArray arrayWithObjects:statusKey, messageKey, dataIdKey, nextKey, b64Key, fileNameKey, nil]];
        NSLog(@"manually created dictionary is ++++++++++++++++++++++++++++++%@",responseDictionary);
        NSLog(@"statusValue is ++++++++++++++++++++++++++++++%@",[responseDictionary valueForKey:statusKey]);
        return responseDictionary;
    }

}
else //previoucly no else handler so put it here
{

// mErrorMessage = @“服务器错误”; // [self stopIndicatorProgressWithError];     } }

jsonObject仅适用于NSString类????不知道有什么问题。 任何帮助?

谢谢&问候。

1 个答案:

答案 0 :(得分:0)

您可以使用适当的Foundation类“优化”代码:

NSString *jsonObject = ...; // your JSON data as string

NSData *jsonData = [jsonObject dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary * responseDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
              options:0 error:&error];

或者,如果您确实需要可变字典:

NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
             options:NSJSONReadingMutableContainers error:&error];
相关问题