-4
可以将某些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类真????不知道有什么问题。 任何帮助?
谢谢&关于。
感谢响应,我是使用您提供相同的代码看到http://stackoverflow.com/questions/18127363/accessing-key-value-pair-from-jsonserialization-class-return-type-which-is-neith – Alok
这就是我创建密钥的原因/值,因此字典手动 – Alok
我不能够得到字典/数组这种方式。请参阅我的编辑,以前我使用NSJsonSerialization类,但我得到NSString的回应这就是为什么我手动创建NSDictionary请看Ny编辑.. – Alok