【发布时间】:2017-08-03 09:03:51
【问题描述】:
我有一个如下所示的 JSON(从 URL 获取)-
{
action :getAllJournal;
data :{
journalList :[{
cancelled : F;
"cust_code" : "700-T022";
"journal_amount" : 2216;
"journal_code" : "JV1603/001";
"journal_date" : "2016-03-15 00:00:00";
"journal_id" : 1;
outstanding : 0;
},
{
cancelled : F;
"cust_code" : "700-0380";
"journal_amount" : 120;
"journal_code" : "JV1605/006";
"journal_date" : "2016-05-31 00:00:00";
"journal_id" : 2;
outstanding : 120;
},
{
cancelled : F;
"cust_code" : "700-T280";
"journal_amount" : 57;
"journal_code" : "JV1609/001";
"journal_date" : "2016-09-22 00:00:00";
"journal_id" : 3;
outstanding : 0;
}
];
};
message = "";
"message_code" = "";
result = 1;}
下面的代码是从 URL 获取 JSON 并将它们存储在 NSMutableArray 中。在将它们存储到数组之前,它工作正常,但我对 JSON 格式有点困惑,不知道如何通过键获取结果。
__block NSMutableArray *jsonArray = nil;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *urlString = [NSString stringWithFormat:@"http://xxxxxxx/api.php?action=getAllJournal"];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError)
{
if (data)
{
id myJSON;
@try {
myJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}
@catch (NSException *exception) {
}
@finally {
}
jsonArray = (NSMutableArray *)myJSON;
NSString *nsstring = [jsonArray description];
NSLog(@"IN STRING -> %@",nsstring);
NSData *data = [nsstring dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
if(jsonObject !=nil){
if(![[jsonObject objectForKey:@"journalList"] isEqual:@""]){
NSMutableArray *array=[jsonObject objectForKey:@"journalList"];
NSLog(@"array: %lu",(unsigned long)array.count);
int k = 0;
for(int z = 0; z<array.count;z++){
NSString *strfd = [NSString stringWithFormat:@"%d",k];
NSDictionary *dicr = jsonObject[@"journalList"][strfd];
k=k+1;
// NSLog(@"dicr: %@",dicr);
NSLog(@"cust_code - journal_amount : %@ - %@",
[NSMutableString stringWithFormat:@"%@",[dicr objectForKey:@"cust_code"]],
[NSMutableString stringWithFormat:@"%@",[dicr objectForKey:@"journal_amount"]]);
}
}
}else{
NSLog(@"Error - %@",jsonError);
}
}
}];
由此,我能够成功获取 JSON。但它总是给我这个错误:Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in an object around character 6." UserInfo={NSDebugDescription=No string key for value in an object around character 6.}如何从journalList获取所有值?我是 iOS 新手,这就是为什么不知道我错过了什么。
【问题讨论】:
-
你可以为记者创建一个模型类,然后使用像github.com/stig/json-framework这样的库来帮助你轻松地将json解析为对象
-
您的 JSON 无效。修复它在此处检查您的 json:jsonlint.com
-
@kapsym 我现在不愿意使用任何库..有没有可能的方法在不使用库的情况下做到这一点?
-
可能是你的 json 序列化没有得到正确的 json 格式。你能准确地告诉你的代码在哪里得到这个错误。
-
NSString *nsstring = [jsonArray description]; NSLog(@"IN STRING -> %@",nsstring); NSData *data = [nsstring dataUsingEncoding:NSUTF8StringEncoding];No 和 No.NSArray *journalList = myJSON[@"data"][@"journalList"],这是一个字典数组。
标签: ios objective-c json