0

I am new to iOS & JSON parsing I'm Getting some JSON data like,

 [
     {
         "id":3,
         "name":"SCORM 0",
         "visible":1,
         "summary":"",
         "summaryformat":1,
         "modules":[
                    {
                        "id":1,
                        "url":"http:\/view.php?id=1",
                        "name":"Course01",
                        "visible":1,
                        "modicon":"http:\theme\/image.php\/standard\/scorm\/1378190687\/icon",
                        "modname":"scorm",
                        "modplural":"SCORM packages",
                        "indent":0
                    },
                    {
                        "id":2,
                        "url":"http:\/\/192.168.4.196\/moodle\/mod\/forum\/view.php?id=2",
                        "name":"News forum",
                        "visible":1,
                        "modicon":"http:\//image.php\/standard\/forum\/1378190687\/icon",
                        "modname":"settle",
                        "modplural":"Forums",
                        "indent":0
                    }
                    ]
     },
     {
         "id":2,
         "url":"http:\/\/view.php?id=2",
         "name":"News forum",
         "visible":1,
         "modicon":"http:\/\theme\/image.php\/standard\/forum\/1378190687\/icon",
         "modname":"forum",
         "modplural":"Forums",
         "indent":0
     }
     ]

I need to separate the data's with respect to "modname" != "forum" and store the respective data's in the array.

Helps and Solutions will be appreciated.

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
Prabhu Natarajan
  • 869
  • 1
  • 9
  • 13
  • You can take a look at a question from yesterday http://stackoverflow.com/questions/18639119/filter-and-store-json-values-in-nsarray/18639240?noredirect=1#comment27465379_18639240 – Vik Sep 06 '13 at 08:48
  • @Vik the problem is i need to find a solution for != value in the keys and store all other values. – Prabhu Natarajan Sep 06 '13 at 08:51
  • If you read the answer I gave in the question I linked, there is what you're looking for – Vik Sep 06 '13 at 08:52
  • see this http://stackoverflow.com/questions/5813077/iphone-ios-json-parsing-tutorial – Vaibhav Gautam Sep 06 '13 at 08:56

2 Answers2

3
NSMutableArray *jsonArray = [[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e] mutableCopy];

jsonArray = [jsonArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    return ![evaluatedObject[@"modname"] isEqualToString:@"forum"];
}];

This is a sketch of what you could do

Vik
  • 1,897
  • 12
  • 18
  • but this code didn't works the output contains id = 2; indent = 0; modicon = "http:/87/icon"; modname = forum; modplural = Forums; name = "News forum"; url = "http:/view.php?id=2"; visible = 1; – Prabhu Natarajan Sep 06 '13 at 10:05
  • Have you put the ! after the return? – Vik Sep 06 '13 at 10:08
  • it shows the O/P like the following { id = 2; indent = 0; modicon = "http:/php/standard/forum/1378190687/icon"; modname = forum; modplural = Forums; name = "News forum"; url = "http://forum/view.php?id=2"; visible = 1; } ); name = "SCORM 0"; summary = ""; summaryformat = 1; visible = 1; } – Prabhu Natarajan Sep 06 '13 at 13:14
  • In the block, try to write: BOOL checked = ![evaluatedObject[@"modname"] isEqualToString:@"forum"]; NSLog(@"Object %@ , checked = %@", evaluatedObject[@"modname"], checked ? @"YES" : @"NO"); return checked; – Vik Sep 06 '13 at 13:20
  • It showing Object (null) , checked = YES in the log and the same O/P for NSLog(@"Filtered %@",self.jsonData); – Prabhu Natarajan Sep 06 '13 at 13:24
  • what's self.jsonData and how do you build it? If it's null, you're doing wrong something before – Vik Sep 06 '13 at 13:25
  • actually it's self.jsonArray – Prabhu Natarajan Sep 06 '13 at 13:30
  • Ok, you're not helping me to help you. I don't know how this is going to have a happy ending if you don't collaborate – Vik Sep 06 '13 at 13:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36939/discussion-between-prabhu-natarajan-and-vik) – Prabhu Natarajan Sep 06 '13 at 13:35
1
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

    NSLog(@"%@",json);

    NSLog(@"%@",delegate.firstArray);

    NSArray * responseArr = json[@"Deviceinfo"];

    for(NSDictionary * dict in responseArr)
    {
        [delegate.firstArray addObject:[dict valueForKey:@"id"]];
        [delegate.secondArray addObject:[dict valueForKey:@"name"]];
        [delegate.thirdArray addObject:[dict valueForKey:@"visible"]];
        [delegate.fourthArray addObject:[dict valueForKey:@"summery"]];

    }

here all data arrange as per your key

Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • How will this filter the array so "forum" modules are not included? – Steve Wilford Sep 06 '13 at 09:32
  • so add another key and get that value into new array like this [delegate.fourthArray addObject:[dict valueForKey:@"forum"]]; – Jitendra Sep 06 '13 at 09:35
  • The OP doesn't want separate arrays for each key, he wants all items excluding modname=="forum" in a single array. I'm also confused about the use of `delegate` in this answer. – Steve Wilford Sep 06 '13 at 09:37
  • i declare that array in APPDelegate.h that's why here i used to delegate. – Jitendra Sep 06 '13 at 09:38