Can you search for JSON using Swifty-JSON?

I am using SwiftyJSON with a lot of JSON. Using SwiftyJSON, is it possible to search for data using a unique ID to retrieve other objects?

For example.

{  
  "places": [  
    {  
      "name": "Place 1",  
      "id": 123,  
    },  
    {  
      "name": "Place 2",  
      "id": 456,  
    }  
]  
}  

      

I want to use id = 123

to get the associated name. That's all, once the JSON has been loaded. Is it possible?

+3


source to share


1 answer


This is from a discreet JSON document on Github:

//If json is .Dictionary
for (key: String, subJson: JSON) in json {
   //Do something you want
}

      

The first element is always a string, even if the JSON is an array

//If json is .Array
//The `index` is 0..<json.count string value
for (index: String, subJson: JSON) in json {
    //Do something you want
}

      



Then you can just check if your id is equal to 123. Then you just loop through the loop, and if the places ["id"] = 123 then you have your objects.

I mean in algo:

for(places in JSON)
{
  if(places["id"] == 123) 
  {
  //my object is object
  }
}

      

+5


source







All Articles