iOS JSON解析返回null

时间:2013-11-28 09:49:03

标签: ios objective-c json parsing

我正在解析一个看起来像这样的JSON:

    [
  {
  "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

和.m这样的

-(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;
}

现在,我有一个约有8000行,450个对象的本地.json文件。 我将它们解析为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.}

3 个答案:

答案 0 :(得分:1)

正如您已经注意到的,您的JSON数据无效。在那种情况下

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

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

你也忘了alloc + init NSMutableArray *array

答案 1 :(得分:0)

我会这样做:

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

答案 2 :(得分:0)

你的JSON应该是

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

根据Apple文档:

  

<强> JSONObjectWithData:选项:错误:

     

错误 如果发生错误,则返回时包含一个NSError对象,   描述了这个问题。

     

返回值 数据中JSON数据的Foundation对象,如果是,则为nil if   发生错误。

error参数设置为nil可能在这种情况下没有帮助。