-4

can some pne please help me in optimize this code:

- (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 = @"Server Error"; // [self stopIndicatorProgressWithError]; } }

jsonObject is true for NSString class only???? do not know what's problem. Any Help?

thanks & regards.

maddy
  • 4,001
  • 8
  • 42
  • 65

1 Answers1

0

You can "optimize" your code by using the appropriate Foundation classes:

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

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

or, if you really need a mutable dictionary:

NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
             options:NSJSONReadingMutableContainers error:&error];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • thanks for response, i was using the same code as you provide see http://stackoverflow.com/questions/18127363/accessing-key-value-pair-from-jsonserialization-class-return-type-which-is-neith – maddy Aug 12 '13 at 10:14
  • thats the reason i am creating key/value thus dictionary manually – maddy Aug 12 '13 at 10:15
  • i am not able to get dictionary/array this way.please see my edit, previously i as using NSJsonSerialization class but i was getting NSString in response thats why i am creating NSDictionary manually please see ny edit.. – maddy Aug 12 '13 at 10:47
  • @Maddy: I assume that your response is "nested JSON": `jsonObject` is a string which itself is the JSON encoded form of a dictionary. In that case my code would also work to convert the `jsonObject` string to a proper dictionary. Compare http://stackoverflow.com/a/16948785/1187415 for a similar issue. – Martin R Aug 12 '13 at 11:03