我解析一個JSON看起來像這樣:iOS的JSON解析返回null
[
{
"Country of Build":"Belgium",
"Shipbuilder":" Boelwerf",
"Hull #":" 1487",
"Ship Name":" Methania",
"Shipowner":" Distrigas",
"Operator":" Exmar",
"Delivery":"Oct-78",
"Flag":" Belgium",
"Class":" LR",
"Power Plant":" Steam",
"HP":" 45,000",
"Speed": 19.0,
"Cargo System":" GT NO 85",
"# of Tanks": 5,
"Capacity ":" 131,235",
"Price":
},
{
"Country of Build":"China",
"Shipbuilder":" Cosco Dalian",
"Hull #":" ",
"Ship Name":" ",
"Shipowner":" CNOOC Energy",
"Operator":" ",
"Delivery":" 1Q15",
"Flag":" ",
"Class":" AB/CC",
"Power Plant":" DFDE",
"HP":" ",
"Speed": ,
"Cargo System":" GT NO 96",
"# of Tanks": 4,
"Capacity ":" 28,000",
"Price":81
}, ---8000 more lines--- }]
我有,我想分析的對象到自定義對象,它看起來是這樣的: .H
@interface LNGVessel : NSObject
@property NSString *countryOfBuild;
@property NSString *shipBuilder;
@property NSString *hull;
@property NSString *shipName;
@property NSString *shipOwner;
@property NSString *shipOperator;
@property NSString *delivery;
@property NSString *flag;
@property NSString *shipClass;
@property NSString *powerPlant;
@property NSString *hp;
@property NSString *speed;
@property NSString *cargoSystem;
@property NSString *numOfTanks;
@property NSString *capacity;
@property NSString *price;
-(id)initWithDict:(NSDictionary*)dictionary;
@end
像這樣
-(id)initWithDict:(NSDictionary *)dictionary{
self = [super init];
if(self)
{
self.countryOfBuild = [dictionary objectForKey:@"Country of Build"];
self.shipBuilder = [dictionary objectForKey:@"Shipbuilder"];
self.hull = [dictionary objectForKey:@"Hull #"];
self.shipName = [dictionary objectForKey:@"Ship Name"];
self.shipOwner = [dictionary objectForKey:@"Shipowner"];
self.shipOperator = [dictionary objectForKey:@"Operator"];
self.delivery = [dictionary objectForKey:@"Delivery"];
self.flag = [dictionary objectForKey:@"Flag"];
self.shipClass = [dictionary objectForKey:@"Class"];
self.powerPlant = [dictionary objectForKey:@"Power Plant"];
self.hp = [dictionary objectForKey:@"HP"];
self.speed = [dictionary objectForKey:@"Speed"];
self.cargoSystem = [dictionary objectForKey:@"Cargo System"];
self.numOfTanks = [dictionary objectForKey:@"# of Tanks"];
self.capacity = [dictionary objectForKey:@"Capacity"];
self.price = [dictionary objectForKey:@"Price"];
}
return self;
}
一個.M現在,我有一個本地上傳.json文件,約8000線,450是h物體。 我分析他們在UIMutableArray類別的數組,它看起來像這樣:
//.h
@interface NSMutableArray (LNGVessels)
+(NSMutableArray*)allVessels;
@end
//.m
@implementation NSMutableArray (LNGVessels)
+(NSMutableArray*)allVessels{
NSMutableArray *array;
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"Vessels" ofType: @"json"];
NSData *data = [[NSData alloc]initWithContentsOfFile:pathToFile];
id JSONArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSEnumerator *enumerator = [JSONArray objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) {
LNGVessel *vessel = [[LNGVessel alloc]initWithDict:item];
[array addObject:vessel];
}
return array;
}
@end
的問題?這不起作用,它總是返回null。我已經登錄NSData的對象,它返回的所有內容的JSON(十六進制)
我認爲這可能是JSON的錯,所以我檢查http://jsonlint.com/並粘貼了整個事情。我得到了一個錯誤
Parse error on line 18:
... "Price": }, { "Co
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
這表明我需要解決這個問題。但是,我仍然認爲我的代碼只會爲該參數插入nil。
哦,我意識到JSONSerilaziation得到了一個錯誤參數。它記錄
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 376.) UserInfo=0x170463380 {NSDebugDescription=Invalid value around character 376.}
它的JSON文檔錯誤?是否有任何工具可以將空對象插入到空的參數中,這樣做可能會產生8000條線會是一種痛苦。 –
我想它會更好,你要確保這是問題所在,首先用更小和完全有效的JSON對象測試你的代碼,如果它確定,那麼銳化你的正則表達式技巧:)你可以查找無效模式並用正則表達式替換它們你懂。 –
我將源代碼粘貼到EXCEL中,將其作爲CSV導出,並將解析後的CSV導出爲JSON,在此過程中,null插入而不是未插入。現在代碼有效,JSON是罪人。 –