-2
我有一些JSON存储为NSString
,我试图将其转换为NSDictionary并在我的JSON中获取'EndDate'的值。从xcode获取存储为NSString的JSON的值
我想从JSON检索'EndDate',但由于水平的数量,我不太确定我应该如何实现它。
这里是JSON:
{ "GetResponse":{ "GetResult":{ "Faults":null, "Response":{ "Asset":{ "AssetParts":{ "@nil":"true" }, "CountryLookupCode":808, "Number”:24234, "Duplicate":"false", "Code":"`123”, "Channel”:”SR”, “Desc”:”Test”, "Number”:123, “Mandate”:{ "@nil":"true" }, “TestTime”:”True”, “Date”:”2010-08-12T19:00:00", “Results”:{ “Details”:[ { "EndDate":"2013-08-13T18:59:59", “Type”:”Taken”, "Item”:”902”, “Level”:”SL”, “Description”:”Timed”, “Group”:1, “Prov”:{ "@nil":"true" }, "StartDate":"2010-08-12T19:00:00" }, { "EndDate":"2013-08-13T18:59:59", “Machine”:”Dated”, “Country”:”UK”, "Code":"CCDD”, “Description”:”Addressed”, “Level”:2, "Provider":{ "@nil":"true" }, "StartDate":"2010-08-12T19:00:00" }, { "EndDate":"2013-08-13T18:59:59", "Type”:”Title”, "ItemNumber”:”1253”, "Service":"NEDD”, “Desc”:”Down”, “Grp”:5, "Provider":{ "@nil":"true" }, "StartDate":"2010-08-12T19:00:00" } ] } } } } } }
这里是我的代码:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://192.168.0.20/test.json"]];
[request setHTTPMethod:@"GET"];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
//NSLog(@"requestReply: %@", requestReply);
//End
NSData *data = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *performersArray = [json objectForKey:@"GetResponse"];
for (NSDictionary *performerDic in performersArray) {
NSLog(@"%@", [performerDic objectForKey:@"EndDate"]);
}
感谢
嗨。检查你的json文件。 'NSArray * performersArray = [json objectForKey:@“GetResponse”];'是错误的。 '[json objectForKey:@“GetResponse”]' - 返回一个NSDictionary。 –
我认为,如果我错了,抱歉,您输入的JSON缺少某些内容。这里是一个URL,你可以检查你的数据,看看你的JSON是如何http://jsonviewer.stack.hu。网站无法转换您的JSON,我说的原因是。 –
'requestReply'不需要,'data'也不需要'requestHandler'。 'NSASCIIStringEncoding'几乎总是一个坏主意,'NSUTF8StringEncoding'几乎总是更好。如果你想从数据使用中得到一个字符串:'initWithData:encoding'。 – zaph