-3
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
 print(json)

My print statement gave following result :

`Result =  [ 
        {
    Friday = 0;
    Monday = 0;
    Saturday = 0;
    Sunday = 0;
    Thursday = 0;
    Tuesday = 0;
    Wednesday = 1;
},
        {
    Friday = 1;
    Monday = 0;
    Saturday = 0;
    Sunday = 0;
    Thursday = 0;
    Tuesday = 0;
    Wednesday = 0;
}]

I want to get the days whose value is 1 (In above JSON I want Wednesday and Friday) How to iterate through this JSON ? Thanks in advance.

swift_USer
  • 159
  • 3
  • 15
  • Does your JSON Valid???Show what have you tried?? – seon Jun 30 '17 at 10:07
  • That is not a json data .. Are you sure about the data? – Macintosh_89 Jun 30 '17 at 10:08
  • Please check my updated Question – swift_USer Jun 30 '17 at 10:13
  • Possible duplicate of [Correctly Parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Dávid Pásztor Jun 30 '17 at 10:15
  • Iterate the dictionaries inside the array and further iterate all keys of the dictionary. Now if the value of the key is 1 add that key in another dictionary with a value 1. Finally print all the keys of the dictionary you created. – Milan Agarwal Jun 30 '17 at 10:16
  • @Milan can you please explain with an example please – swift_USer Jun 30 '17 at 10:22
  • `json.map({$0.value.map({$0.filter{($0.value == 1)}})})`, if JSON is correctly defined as `[String:[[String:Int]]]`. But according to your level of knownledge, I'd go with a simpler approach: Iteration, and it's unclear the structure you want to keep at the end. – Larme Jun 30 '17 at 10:23

2 Answers2

-1

First Check your data is valid JSON. if it's then use below looping to get days

     var daysList = [String]()

     for dict in Result {
           for values in dict {
              if let days = values.value as? Int,let values.value! == 1 {
                    daysList.append(days)
               }else{
                  //Sometimes 0 and 1 values doesn't casting as? Int.So probably you can try out with Bool
                  if let days = values.value as? Bool,let values.value! == 1 {
                     daysList.append(days)
                  }
               }
            }
        }

print(daysList) // Wednesday,Friday
Dharma
  • 3,007
  • 3
  • 23
  • 38
-2

Here is an example in objective c:

///Parse Json
NSArray *arrResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];

///Iterate data and form a dictionary with days having 1
NSMutableDictionary *dictDays = [NSMutableDictionary dictionary];

for (NSDictionary *dictWeek in arrResult) {

    for (NSString *strDay in [dictWeek allKeys]) {

        if ([[dictWeek objectForKey:strDay]intValue] == 1) {

            [dictDays setObject:@1 forKey:strDay];

        }

    }
}

///Print the days having 1
for (NSString *strDay in [dictDays allKeys]) {

    NSLog(@"Day Name: %@",strDay);
}
Milan Agarwal
  • 427
  • 2
  • 15