Thiha Aung
Thiha Aung

Reputation: 5096

how to add json object to dictionary

when i create output with Alamofire using SwiftyJSON

Alamofire.request(.POST, "http://localhost:8080/ws/automobile/global/auction/latest/venues").responseJSON() {
        (request, response, jsonData, error) in

        var venues = JSON(jsonData!)

        println(venues)
    }

it appear like this in console

{
  "C2058" : [
    "LAA Okayama"
  ],
  "C2062" : [
    "NAA Osaka"
  ],
  "C2035" : [
    "JU Ibaraki"
  ],
  "C2526" : [
    "SMAP Fukuoka Nyusatsu"
  ],
  "C2530" : [
    "SMAP Tokyo Nyusatsu"
  ],
  "C2074" : [
    "TAA Tohoku"
  ],
  "C2008" : [
    "BCN"
  ],
  "C2012" : [
    "CAA Tokyo"
  ],
  "C2503" : [
    "L-Up PTokyoNyusatsu"
  ],
  "C2047" : [
    "JU Shizuoka"
  ],
  "C2051" : [
    "JU Yamaguchi"
  ],
  "C2086" : [
    "USS Saitama"
  ]
}

I want to add this to my new dictionary to use in UIPickerView,any suggestion how to do it.I am newbie to swift,code answer is really appreciated.Thank you.

What i Really want it dictionary like this ["C2047":"L-Up PTokyoNyusatsu","C2086":"USS Saitama".......] Please help!!!

Upvotes: 0

Views: 1480

Answers (2)

ljk321
ljk321

Reputation: 16770

Don't know whether this is what you want:

var result = [String:String]()
let d = json.dictionaryValue

for (k, v) in d {
    result[k] = v.arrayValue[0].stringValue
}

println(result)

Upvotes: 2

ohyes
ohyes

Reputation: 3328

From the example you've given:

"C2058" : [ "LAA Okayama" ], "C2062" : [ "NAA Osaka" ], "C2035" : [ "JU Ibaraki" ]

Those "C2058","C2062" are the key of Dictionary. And ["LAA Okayama"] is a String inside NSArray. So, if you want to get the String inside NSArray. First, use the key to get value in Dictionary. Then, use NSArray to get the String.

it would like this:

var dict:NSDictionary=jsonData as Dictionary
var arr=dict["C2058"] as NSArray

and

println(arr[0]) //you'll see LAA Okayama

Upvotes: 0

Related Questions