2013-11-28 66 views
0

我解析一个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.} 

回答

0

你的JSON应该是

{ 
... 
"Price":null 
... 
} 

根据苹果文档:

JSONObjectWithData:options:error:

error If an error occurs, upon return contains an NSError object that describes the problem.

Return Value A Foundation object from the JSON data in data, or nil if an error occurs.

设置error参数nil可能不会在这样的时候有所帮助。

+0

它的JSON文档错误?是否有任何工具可以将空对象插入到空的参数中,这样做可能会产生8000条线会是一种痛苦。 –

+0

我想它会更好,你要确保这是问题所在,首先用更小和完全有效的JSON对象测试你的代码,如果它确定,那么锐化你的正则表达式技巧:)你可以查找无效模式并用正则表达式替换它们你懂。 –

+1

我将源代码粘贴到EXCEL中,将其作为CSV导出,并将解析后的CSV导出为JSON,在此过程中,null插入而不是未插入。现在代码有效,JSON是罪人。 –

0

我会做这样的:

NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
for (NSDictionary *dict in array) { 
    LNGVessel *vessel = [[LNGVessel alloc]initWithDict:dict]; 
    .... 
} 
+0

我做了,但其他一些问题,我来了,建议使用像这样的快速枚举,所以我认为这是更好的。 –

1

正如你已经注意到了,你的JSON数据是无效的。

id JSONArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

只是返回nil。 (它不会省略或跳过无效条目。)

而且您还忘记了alloc + initNSMutableArray *array