json 到 objective-C 中的 nsmutablearray

json to nsmutablearray in objective-C

我们有 json 文件 (content.json)。我们需要从 .json 文件中获取数据,然后放入 NSMutablearray。我们这样试过

 NSString* path  = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"json"];

NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

 NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

   id allKeys = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONWritingPrettyPrinted error:&jsonError];
NSLog(@"Key ***  %@",allKeys);

    for (int i=0; i<[allKeys count]; i++) {
        NSDictionary *arrayResult = [allKeys objectAtIndex:i];
        NSLog(@"name=%@",[arrayResult objectForKey:@"Description"]);

    }

allKeys 值为空

JsonString is


    [
          {
          "ID": “2”,
          "MessageType": "0",
          "Description": "Promotions”,
          "ExpiryDateTime": "2015-07-10 12:00:00", 
          "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"jewellery.jpg\"\/><h3>"Hot" Items<ul \/><\/div>"
          },
          {
          "ID": "4",
          "MessageType": "1",
          "Description": "Offers”,
          "ExpiryDateTime": "2015-07-09 2:00:00", 
          "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"Williams-Petersan.jpg\"\/><h3>"Hot" Items - 2<ul \/><\/div>"
           }
           {
           "ID": “6”,
          "MessageType": “2”,
          "Description": "Invitations”,
          "ExpiryDateTime": "2015-07-09 3:00:00", 
          "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"Puma.jpg\"\/><h3>"Hot" Items - 2<ul \/><\/div>"
           }
]

我们需要将描述数据放入 ArrayResult

请给我们指导

您的 json 无效 - 请注意 ID 2 或 6 周围的 unicode 引号 - ID 4 没问题 - 还有更多错误可以轻松修复 :) 例如通过使用一些在线子验证器网站/支持的编辑器

代码本身看起来没问题

但是

它不会是可变数组 ;) 它将是不可变的。如果你想要可变性,将 NSJSONReadingMutableContainers 传递给 JSON

您不需要在 NSData 之前完成 NSString。只需使用 class 方法 dataWithContentsOfFile: 即可。要使数组可变,只需在序列化时添加命令 mutableCopy

NSString* path  = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"json"];
NSData* jsonData = [NSData dataWithContentsOfFile:path];
id allKeys = [[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]mutableCopy];
NSLog(@"Key ***  %@", allKeys);

for (int i=0; i<[allKeys count]; i++) {
    NSDictionary *arrayResult = [allKeys objectAtIndex:i];
    NSLog(@"name = %@",[arrayResult objectForKey:@"Description"]);
}

这里是正确的 json- 将与代码一起使用的数据:

[
    {
        "ID": "2",
        "MessageType": "0",
        "Description": "Promotions",
        "ExpiryDateTime": "2015-07-10 12:00:00",
        "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"jewellery.jpg\"\/><h3>\"Hot\" Items<ul \/><\/div>"
    },
    {
        "ID": "4",
        "MessageType": "1",
        "Description": "Offers",
        "ExpiryDateTime": "2015-07-09 2:00:00",
        "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"Williams-Petersan.jpg\"\/><h3>\"Hot\" Items - 2<ul \/><\/div>"
    },
    {
        "ID": "6",
        "MessageType": "2",
        "Description": "Invitations",
        "ExpiryDateTime": "2015-07-09 3:00:00",
        "Html": "<div class=\"ad_box\"><img class=\"banner\" src=\"Puma.jpg\"\/><h3>\"Hot\" Items - 2<ul \/><\/div>"
    },

]