-3

{ "pages": 1, "posts": [ { "id": 2345, "categories": [], "tags": [], "author": { "id": 1, "slug": "admin", "url": "", "description": "" }, "comments": [], "custom_fields": { "slide_template": [ "default" ], "request_status": [ "Pending Review" ], "branch": [ "Branch B" ], "assigned_to": [ "Managing Director" ], "category": [ "Equipment" ], "priority": [ "High" ] } },

this is my swift coding till "custom_fields" i can able to fetch the value but after that i cant able to please anyone help me

let url = NSURL(string: urlForCharts)
     do{
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let data = data {
                if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
            if let postArray = jsonObj!.value(forKey: "posts") as? NSArray {
               for status in postArray{
                   if let tagsDict = status as? NSDictionary {
                if let tagsArray =  tagsDict.value(forKey: "custom_fields") as? NSArray {

                        for statusArray in tagsArray{

                            if let statusDict = statusArray as? NSDictionary {
                        if let status = statusDict.value(forKey: "request_status") {

                            self.ticketStatus.append((status as? String)!)
                            self.ticketStatus = self.ticketStatus.sorted(by: <)
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
Sachu_
  • 9
  • 3
  • 1
    See the "Some general considerations to parse JSON" part of the accepted answer in the duplicate link. You will learn how to read JSON and understand the difference between JSON dictionary and JSON array. – Eric Aya Feb 16 '18 at 10:26
  • 1
    You can use `Decodable` protocol with a custom `struct` to parse the JSON which will allow you to do operations more easily. And as soon as you're in **Swift**, use Swift's native types. ___Just don't use NS types___. – nayem Feb 16 '18 at 10:51

2 Answers2

-1

Your custom_fields is type of Dictionary not NSArray.

"custom_fields": {
                    "slide_template": ["default"],
                    "request_status": ["Pending Review"],
                    "branch": ["Branch B"],
                    "assigned_to": ["Managing Director"],
                    "category": ["Equipment"],
                    "priority": ["High"]
                }

So it should be like this :

if let tagsDictionary =  tagsDict.value(forKey: "custom_fields") as? NSDictionary {
   if let slide_template = tagsDictionary.value(forKey: "slide_template") as? NSArray{
   }
   if let request_status = tagsDictionary.value(forKey: "request_status") as? NSArray{
       if let resquestStatus = request_status.first as? String{
         //append this value to your array
       } 
   }
}
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
-1

Try this

if let tagsArray =  tagsDict.value(forKey: "custom_fields") as? NSDictionary {
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29