Search code examples
iosjsonswiftafnetworkingswifty-json

Retrieval of title from json from indexed object array ios using swiftyjson


JSON

    {
  "321" : {
    "title" : "xyz",
    "pageid" : 321,
    "ns" : 0
  },
  "172" : {
    "pageimage" : "xyzc.jpg",
    "pageid" : 172,
    "title" : "xyzcc",
    "ns" : 0,
    "thumbnail" : {
      "width" : 100,
      "height" : 57,
      "source" : "https:\/\/upload.abcd.org\/wikipedia\/commons\/thumb\/d\/de\/xyz.jpg\/100px-xyz.jpg"
    }
  },
  "224" : {
    "pageimage" : "abc.jpg",
    "pageid" : 224,
    "title" : "dasf",
    "ns" : 0,
    "thumbnail" : {
      "width" : 98,
      "height" : 100,
      "source" : "http:\/\/example.org\/images\/thumb\/2\/26\/skfdb.jpg\/98px-586px-dasdfsa.jpg"
    }
  },
  "825" : {
    "title" : "efkjdsb",
    "pageid" : 825,
    "ns" : 0
  },
  "229" : {
    "title" : "eafewg",
    "pageid" : 229,
    "ns" : 0
  }

How can I access the "title" and "thumbnail": "source" in the JSON file after retrieval from the JSONresponse using SwiftyJSON?

"321", "172" can change and depends. There multiple number of more objects in the array of same format.

Some objects have thumbnail object some don't as well.

How shall I fetch title and and thumbnail->source to a TableView with image and label to create a list.

(Using Swift language) Im Retrieving json using AFNetworking

let manager = AFHTTPSessionManager()
        manager.GET(url, parameters: nil, success: {(operation, responseObject) -> Void in

            let responsejson = JSON(responseObject!)


            print("\n \n \n \n \n \n  Retrieved = \n\(responsejson)")

            let items = responsejson.count
            print("Items : \(items)")

Thanks in Advance.


Solution

  • You can use a code like this to get each value:

    for (key,obj) in responsejson {
        print(obj["title"])
        print(obj["thumbnail"]["source"])
    
    }