swift 3.0 query in json search value return pair associated

Viewed 85

I have just spent hours trying to understand how a JSON works. I give up, can you guys help me out?

Here's what I need. I have a local JSON that looks like this:

[
     {
       "partnumber": "1",
       "description": "part#1"
     },
     {
       "partnumber": "2",
       "description": "part#2"
     },
     {
       "partnumber": "3",
       "description": "part#3"
     }
]

and I am trying send a query to that JSON if textfield.text ="1" return the value of description.

I can open the file and parse it but I am stuck there

private func readJson() {
        do {
            if let file = Bundle.main.url(forResource: "convertcsv", withExtension: "json") {
                let data = try Data(contentsOf: file)
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                if let object = json as? [Any] {

                // what to do here ???????????
                print(object)

                } else {
                    print("JSON is invalid")
                }
            } else {
                print("no file")
            }
        } catch {
            print(error.localizedDescription)
        }
    }

I have seen that we can filter the JSON, would it work in that case or do we have to use a loop?

1 Answers
Related