2012-07-15 79 views
1

我有一个JSON提要:NSJSONSerialization可以处理null吗?

{ 
    "createEntities": [ 
     { 
      "username": "hackImport", 
      "deleted": false, 
      "myName": "Stocks", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "132256ef-1e94-483f-a87d-35caeee636db", 
      "dateModified": "2012-04-13T08:06:17.077971-04:00", 
      "sortKey": 5, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "hackImport", 
      "deleted": false, 
      "myName": "Foreign Currency", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "43510d43-f17a-4d06-9138-0a304b9f09ff", 
      "dateModified": "2012-04-13T08:06:17.077971-04:00", 
      "sortKey": 2, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "adamek", 
      "deleted": false, 
      "myName": "Real Estate", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "57a365e0-fa9c-4e47-b77b-5a3f23d3dbee", 
      "dateModified": "2012-04-22T14:33:43.464506-04:00", 
      "sortKey": 3, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "adamek", 
      "deleted": false, 
      "myName": "Mutual Funds", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "8bd3f71c-9255-4505-87f7-a48a2665d366", 
      "dateModified": "2012-05-01T19:28:15.897377-04:00", 
      "sortKey": 7, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "hackImport", 
      "deleted": false, 
      "myName": "U.S. Currency", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "ea2a086c-ddbd-4b75-ac29-130626a4f11b", 
      "dateModified": "2012-04-13T08:06:17.077971-04:00", 
      "sortKey": 1, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "hackImport", 
      "deleted": false, 
      "myName": "Other Property", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "ec04d62e-97bf-4c0d-b64e-4b212fda6215", 
      "dateModified": "2012-04-13T08:06:17.077971-04:00", 
      "sortKey": 4, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     }, 
     { 
      "username": "hackImport", 
      "deleted": false, 
      "myName": "Bonds", 
      "dateSynced": "2012-07-05T12:00:00-04:00", 
      "uuidKey": "fad7d556-c922-49c3-b7dd-ffa96eab9c55", 
      "dateModified": "2012-04-13T08:06:17.077971-04:00", 
      "sortKey": 6, 
      "dateCreated": "2012-04-13T08:06:17.077971-04:00" 
     } 
    ], 
    "modifyEntities": null, 
    "deleteEntities": null, 
    "entity": "CommodityTypes" 
} 

http://jsonlint.com说,这是有效的。

但是:

[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError] 

-[__NSDictionaryM length]: unrecognized selector sent to instance 0x8ba2200 错误失败。

json是由Python 3.2使用json.dumps在服务器上生成的。 json.dumps将Python None转换为json字符串中的null。

任何想法为什么这是失败?

+0

你的对象'jsonData'的类是什么? – 2012-07-15 16:01:18

回答

1

您的jsonData被取消分配。检查jsonData是否仍然是有效的指针。

6

是,NSJSONSerialization可以处理null,检查以下内容:

NSString *input = @"{ \"value\": null }"; 
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding]; 

NSError *error = nil; 
id JSONValue = [[NSJSONSerialization JSONObjectWithData:data options:0 error:&error] objectForKey:@"value"]; 

NSLog(@"(%@), %@ error: %@", [JSONValue class], JSONValue, error); 

输出:

 
2012-07-15 12:06:55.614 TestProj[25329:503] (NSNull), &ltnull> error: (null) 

这意味着你的问题不在于此,而如果我是冒险猜测,jsonData是实例NSMutableDictionary的对象,而不是NSData