-2

I am having trouble with NSDictionary that i generated from my json.I dont know whats wrong with my NSDictionary which mean i dont get the same sorted keys like json in my NSDictionary when i tried to output it.Please help

Here is my json that i tested in intellij IDEA REST tools

{"0":["All Venues"],"1_190":["AEP Gifu"],"2_69":["ARAI Bayside"],"3_61":["ARAI Oyama"],"4_86":["ARAI Oyama VT"],"5_35":["ARAI Sendai"],"6_100":["AS Members"],"7_88":["AS Oneprice"],"8_17":["AUCNET"],"9_208":["Auct Kyouyuu"],"10_39":["BAYAUC"],"11_40":["BCN"],"12_209":["BCN Kyouyuu"],"13_37":["CAA Chubu"],"14_26":["CAA Gifu"],"15_185":["CAA Kyouyuu"],"16_25":["CAA Tokyo"],"17_101":["CAA Touhoku"],"18_109":["GAO Stock"],"19_158":["GE Fukuoka"],"20_159":["GE Kobe"],"21_157":["GE Tokyo"],"22_134":["GNN"],"23_75":["HAA Kobe"],"24_49":["HAA Osaka (Hanaten)"],"25_206":["Hana Auction"],"26_207":["Hana Stock"],"27_43":["HERO"],"28_200":["Hino Hansha"],"29_198":["Hino Hidaka"],"30_199":["Hino Kobe"],"31_18":["Honda Fukuoka"],"32_22":["Honda Hokkaido"],"33_19":["Honda Kansai"],"34_20":["Honda Nagoya"],"35_99":["Honda Sendai"],"36_21":["Honda Tokyo"],"37_160":["IAA Osaka"],"38_149":["Ippatsu Stock"],"39_150":["Isuzu Kobe"],"40_180":["Isuzu Kyouyuu"],"41_151":["Isuzu Kyushu"],"42_155":["Isuzu Makuhari"],"43_38":["JAA"],"44_178":["JAA HAA"],"45_70":["JAA Tsukuba"],"46_205":["JCRA"],"47_55":["JU Aichi"],"48_195":["JU Akita"],"49_68":["JU Chiba"],"50_64":["JU Fukuoka"],"51_59":["JU Fukushima"],"52_79":["JU Gifu"],"53_58":["JU Gunma"],"54_56":["JU Hiroshima"],"55_44":["JU Ibaraki"],"56_45":["JU Ishikawa"],"57_60":["JU Kanagawa"],"58_193":["JU Kumamoto"],"59_33":["JU Mie"],"60_66":["JU Miyagi"],"61_204":["JU Miyazaki"],"62_32":["JU Nagano"],"63_80":["JU Nara"],"64_67":["JU Niigata"],"65_217":["JU Oita"],"66_65":["JU Okayama LAA"],"67_128":["JU Okinawa"],"68_30":["JU Saitama"],"69_57":["JU Sapporo"],"70_31":["JU Shizuoka"],"71_73":["JU Tochigi"],"72_23":["JU Tokyo"],"73_127":["JU Toyama"],"74_201":["JU Yamagata"],"75_34":["JU Yamaguchi"],"76_179":["JU Yamanashi"],"77_145":["KAA"],"78_51":["KCAA Fukuoka"],"79_42":["KCAA M Kyushu"],"80_72":["KCAA Yamaguchi"],"81_147":["Kyouyuu Stock"],"82_191":["LAA Challenge"],"83_50":["LAA Kansai"],"84_41":["LAA Shikoku"],"85_181":["NAA Fukuoka"],"86_53":["NAA Nagoya"],"87_87":["NAA Nagoya Nyu"],"88_102":["NAA Osaka"],"89_52":["NAA Osaka"],"90_71":["NAA Tokyo"],"91_82":["NAA Tokyo Nyuusatsu"],"92_163":["NPS Fukuoka"],"93_189":["NPS Gifu"],"94_154":["NPS Osaka"],"95_182":["NPS Oyama"],"96_162":["NPS Sendai"],"97_153":["NPS Tokyo"],"98_192":["NPS Tomakomai"],"99_133":["ORIX Atsugi"],"100_152":["ORIX Fukuoka"],"101_164":["ORIX IP Stock"],"102_131":["ORIX Kobe"],"103_135":["ORIX Nagoya"],"104_132":["ORIX Sendai"],"105_54":["SAA Hamamatsu"],"106_29":["SAA Sapporo"],"107_184":["Sakura"],"108_167":["Shisuroke Kobe"],"109_171":["Shisuroke Kyusyu"],"110_168":["Shisuroke Nagoya"],"111_166":["Shisuroke NCS Kobe"],"112_170":["Shisuroke Tokyo"],"113_174":["SMAP Fukuoka"],"114_175":["SMAP Kobe"],"115_176":["SMAP Nagoya"],"116_173":["SMAP Sapporo"],"117_172":["SMAP Tokyo"],"118_146":["Syoudan Stock"],"119_47":["TAA Chubu"],"120_103":["TAA Hiroshima"],"121_141":["TAA Hokkaido"],"122_46":["TAA Kantou"],"123_27":["TAA Kinki"],"124_183":["TAA Kyouyuu"],"125_28":["TAA Kyushu"],"126_84":["TAA Minamikyu"],"127_156":["TAA Shikoku"],"128_48":["TAA Touhoku"],"129_81":["TAA Yokohama"],"130_202":["TAU Auction"],"131_203":["TAU Stock"],"132_15":["USS Fukuoka"],"133_3":["USS Gunma"],"134_108":["USS Hokuriku"],"135_13":["USS Kobe"],"136_16":["USS Kyushu"],"137_10":["USS Nagoya"],"138_122":["USS Niigata"],"139_14":["USS Okayama"],"140_12":["USS Osaka"],"141_11":["USS R Nagoya"],"142_7":["USS Saitama"],"143_1":["USS Sapporo"],"144_9":["USS Shizuoka"],"145_144":["USS Stock"],"146_2":["USS Tohoku"],"147_5":["USS Tokyo"],"148_8":["USS Yokohama"],"149_197":["WCN-N"],"150_214":["Zero Central"],"151_212":["Zero Chiba"],"152_215":["Zero Hakata"],"153_210":["Zero Hokkaido"],"154_216":["Zero Osaka"],"155_211":["Zero Sendai"],"156_213":["Zero Shonan"],"157_142":["ZIP Osaka"]}

As you see the keys are comes with 0,1_190,2_69,3_61,4_86,etc and values are "All Venues,AEP Gifu,ARAI Bayside,(which will go to A,B,C,D in Alphabetical order...)"

But when i created output of json after i added to NDSictionary using NSJSONSerialization,it appear like that

Optional({
0 =     (
    "All Venues"
);
"100_152" =     (
    "ORIX Fukuoka"
);
"101_164" =     (
    "ORIX IP Stock"
);
"102_131" =     (
    "ORIX Kobe"
);
"103_135" =     (
    "ORIX Nagoya"
);
"104_132" =     (
    "ORIX Sendai"
);
"105_54" =     (
    "SAA Hamamatsu"
);
"106_29" =     (
    "SAA Sapporo"
);
"107_184" =     (
    Sakura
);
"108_167" =     (
    "Shisuroke Kobe"
);
"109_171" =     (
    "Shisuroke Kyusyu"
);
"10_39" =     (
    BAYAUC
);
"110_168" =     (
    "Shisuroke Nagoya"
);
"111_166" =     (
    "Shisuroke NCS Kobe"
);
"112_170" =     (
    "Shisuroke Tokyo"
);
"113_174" =     (
    "SMAP Fukuoka"
);
"114_175" =     (
    "SMAP Kobe"
);
"115_176" =     (
    "SMAP Nagoya"
);
"116_173" =     (
    "SMAP Sapporo"
);
"117_172" =     (
    "SMAP Tokyo"
);
"118_146" =     (
    "Syoudan Stock"
);
"119_47" =     (
    "TAA Chubu"
);
"11_40" =     (
    BCN
);
"120_103" =     (
    "TAA Hiroshima"
);
"121_141" =     (
    "TAA Hokkaido"
);
"122_46" =     (
    "TAA Kantou"
);
"123_27" =     (
    "TAA Kinki"
);
"124_183" =     (
    "TAA Kyouyuu"
);
"125_28" =     (
    "TAA Kyushu"
);
"126_84" =     (
    "TAA Minamikyu"
);
"127_156" =     (
    "TAA Shikoku"
);
"128_48" =     (
    "TAA Touhoku"
);
"129_81" =     (
    "TAA Yokohama"
);
"12_209" =     (
    "BCN Kyouyuu"
);
"130_202" =     (
    "TAU Auction"
);
"131_203" =     (
    "TAU Stock"
);
"132_15" =     (
    "USS Fukuoka"
);
"133_3" =     (
    "USS Gunma"
);
"134_108" =     (
    "USS Hokuriku"
);
"135_13" =     (
    "USS Kobe"
);
"136_16" =     (
    "USS Kyushu"
);
"137_10" =     (
    "USS Nagoya"
);
"138_122" =     (
    "USS Niigata"
);
"139_14" =     (
    "USS Okayama"
);
"13_37" =     (
    "CAA Chubu"
);
"140_12" =     (
    "USS Osaka"
);
"141_11" =     (
    "USS R Nagoya"
);
"142_7" =     (
    "USS Saitama"
);
"143_1" =     (
    "USS Sapporo"
);
"144_9" =     (
    "USS Shizuoka"
);
"145_144" =     (
    "USS Stock"
);
"146_2" =     (
    "USS Tohoku"
);
"147_5" =     (
    "USS Tokyo"
);
"148_8" =     (
    "USS Yokohama"
);
"149_197" =     (
    "WCN-N"
);
"14_26" =     (
    "CAA Gifu"
);
"150_214" =     (
    "Zero Central"
);
"151_212" =     (
    "Zero Chiba"
);
"152_215" =     (
    "Zero Hakata"
);
"153_210" =     (
    "Zero Hokkaido"
);
"154_216" =     (
    "Zero Osaka"
);
"155_211" =     (
    "Zero Sendai"
);
"156_213" =     (
    "Zero Shonan"
);
"157_142" =     (
    "ZIP Osaka"
);
"15_185" =     (
    "CAA Kyouyuu"
);
"16_25" =     (
    "CAA Tokyo"
);
"17_101" =     (
    "CAA Touhoku"
);
"18_109" =     (
    "GAO Stock"
);
"19_158" =     (
    "GE Fukuoka"
);
"1_190" =     (
    "AEP Gifu"
);
"20_159" =     (
    "GE Kobe"
);
"21_157" =     (
    "GE Tokyo"
);
"22_134" =     (
    GNN
);
"23_75" =     (
    "HAA Kobe"
);
"24_49" =     (
    "HAA Osaka (Hanaten)"
);
"25_206" =     (
    "Hana Auction"
);
"26_207" =     (
    "Hana Stock"
);
"27_43" =     (
    HERO
);
"28_200" =     (
    "Hino Hansha"
);
"29_198" =     (
    "Hino Hidaka"
);
"2_69" =     (
    "ARAI Bayside"
);
"30_199" =     (
    "Hino Kobe"
);
"31_18" =     (
    "Honda Fukuoka"
);
"32_22" =     (
    "Honda Hokkaido"
);
"33_19" =     (
    "Honda Kansai"
);
"34_20" =     (
    "Honda Nagoya"
);
"35_99" =     (
    "Honda Sendai"
);
"36_21" =     (
    "Honda Tokyo"
);
"37_160" =     (
    "IAA Osaka"
);
"38_149" =     (
    "Ippatsu Stock"
);
"39_150" =     (
    "Isuzu Kobe"
);
"3_61" =     (
    "ARAI Oyama"
);
"40_180" =     (
    "Isuzu Kyouyuu"
);
"41_151" =     (
    "Isuzu Kyushu"
);
"42_155" =     (
    "Isuzu Makuhari"
);
"43_38" =     (
    JAA
);
"44_178" =     (
    "JAA HAA"
);
"45_70" =     (
    "JAA Tsukuba"
);
"46_205" =     (
    JCRA
);
"47_55" =     (
    "JU Aichi"
);
"48_195" =     (
    "JU Akita"
);
"49_68" =     (
    "JU Chiba"
);
"4_86" =     (
    "ARAI Oyama VT"
);
"50_64" =     (
    "JU Fukuoka"
);
"51_59" =     (
    "JU Fukushima"
);
"52_79" =     (
    "JU Gifu"
);
"53_58" =     (
    "JU Gunma"
);
"54_56" =     (
    "JU Hiroshima"
);
"55_44" =     (
    "JU Ibaraki"
);
"56_45" =     (
    "JU Ishikawa"
);
"57_60" =     (
    "JU Kanagawa"
);
"58_193" =     (
    "JU Kumamoto"
);
"59_33" =     (
    "JU Mie"
);
"5_35" =     (
    "ARAI Sendai"
);
"60_66" =     (
    "JU Miyagi"
);
"61_204" =     (
    "JU Miyazaki"
);
"62_32" =     (
    "JU Nagano"
);
"63_80" =     (
    "JU Nara"
);
"64_67" =     (
    "JU Niigata"
);
"65_217" =     (
    "JU Oita"
);
"66_65" =     (
    "JU Okayama LAA"
);
"67_128" =     (
    "JU Okinawa"
);
"68_30" =     (
    "JU Saitama"
);
"69_57" =     (
    "JU Sapporo"
);
"6_100" =     (
    "AS Members"
);
"70_31" =     (
    "JU Shizuoka"
);
"71_73" =     (
    "JU Tochigi"
);
"72_23" =     (
    "JU Tokyo"
);
"73_127" =     (
    "JU Toyama"
);
"74_201" =     (
    "JU Yamagata"
);
"75_34" =     (
    "JU Yamaguchi"
);
"76_179" =     (
    "JU Yamanashi"
);
"77_145" =     (
    KAA
);
"78_51" =     (
    "KCAA Fukuoka"
);
"79_42" =     (
    "KCAA M Kyushu"
);
"7_88" =     (
    "AS Oneprice"
);
"80_72" =     (
    "KCAA Yamaguchi"
);
"81_147" =     (
    "Kyouyuu Stock"
);
"82_191" =     (
    "LAA Challenge"
);
"83_50" =     (
    "LAA Kansai"
);
"84_41" =     (
    "LAA Shikoku"
);
"85_181" =     (
    "NAA Fukuoka"
);
"86_53" =     (
    "NAA Nagoya"
);
"87_87" =     (
    "NAA Nagoya Nyu"
);
"88_102" =     (
    "NAA Osaka"
);
"89_52" =     (
    "NAA Osaka"
);
"8_17" =     (
    AUCNET
);
"90_71" =     (
    "NAA Tokyo"
);
"91_82" =     (
    "NAA Tokyo Nyuusatsu"
);
"92_163" =     (
    "NPS Fukuoka"
);
"93_189" =     (
    "NPS Gifu"
);
"94_154" =     (
    "NPS Osaka"
);
"95_182" =     (
    "NPS Oyama"
);
"96_162" =     (
    "NPS Sendai"
);
"97_153" =     (
    "NPS Tokyo"
);
"98_192" =     (
    "NPS Tomakomai"
);
"99_133" =     (
    "ORIX Atsugi"
);
"9_208" =     (
    "Auct Kyouyuu"
);
})

As you see,it was not like my json before.I dont know why and how I get the data like that.If I want to get the same data sorted like json,how do I have to do that?Please Help....I am stucking this problem for two days.And I don't have the answer yet.I tried all the sorting method to my keys and values which still not effective.Hope this code can help.

var venueValue = [String]()
var venue = [String:String]()

var selectedValue : String?

override func viewDidLoad() {

    self.setNavigationBarItem()
    self.venuePicker!.hidden = true
    self.venueTxt?.delegate = self
    self.venuePicker?.delegate = self

    var request = NSMutableURLRequest(URL: NSURL(string: "http://dev.colay.net/ws/m/autos/global/venues")!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"

    var err: NSError?

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        println("Auction : Request Completed")

        var err: NSError?

        if error == nil{

            var venues = NSJSONSerialization.JSONObjectWithData(data, options:nil, error: &err) as! NSDictionary

            for (k, v) in venues {

                if let value = v as? [String] {
                    //println(value)
                    // Acutally value should start from "All Venue".I wonder why it start at "ZeroCentral",Please check my output.
                    self.venueValue.append(value[0])
                }
            }
            self.venueValue.sort(<)
            println(self.venueValue)

        }
        else{
            println(error.localizedDescription)
        }

        if(err != nil) {
            // If there is an error parsing JSON, print it to the console
            println("JSON Error \(err!.localizedDescription)")
        }

    })

    task.resume()
}

[Updated] The values of the NSDictionary that i tried to output which is sorted ascending in swift term start from this.But,I want "All Venues" to start instead of "AEP Gifu".How do i need to do?

Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
  • That is how your json looks like in Dictionary format. – Arbitur May 29 '15 at 07:36
  • 1
    Try to use it and not just print it put, try to access something and you'll see that there is nothing wrong. – Arbitur May 29 '15 at 07:36
  • The data looks good to me. What were you expecting? – saurabh May 29 '15 at 07:45
  • 1) You can't sort a dictionary, but... http://stackoverflow.com/a/30520701/2227743 -- 2) This might help you understand what's happening: http://stackoverflow.com/a/30125094/2227743 – Eric Aya May 29 '15 at 08:03
  • Oh you mean the sorting... make it more readable in your question instead of this great wall of text. An NSDictionary's keys are not an array, so they will not be in the order you put them in. Instead change your json to an array if order is important. – Arbitur May 29 '15 at 08:07
  • If order is imortant you cannot use an NSDictionary because their keys are arbitrary. – Arbitur May 29 '15 at 08:11
  • Make it an array and inside every index there is a dictionary with 1 key and 1 value. – Arbitur May 29 '15 at 08:41
  • See my answer please – Arbitur May 29 '15 at 09:33

1 Answers1

1

If order is important you cannot rely on an NSDictionary because its keys are arbitrary.

To solve this ordering problem you would want to use an array instead.

So change you json structure to:

{
    "data":[
        {"0":["All Venues"]},
        {"1_190":["AEP Gifu"]},
        {"2_69":["ARAI Bayside"]}
    ]
}

And when converted you will have a [String:[[String:[String]]]] structure.

let jsonDict = ... as! [String:[[String:[String]]]]
let data = jsonDict["data"]! // [[String:[String]]]

for a in data {
    let key = a.keys.first!
    let value = a[key]! // [String]

    for string in value {
        println(string)
    }
}

// Prints out:
// All Venues
// AEP Gifu
// ARAI Bayside
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • I am sorry that i cant respond you back.I tried it,it show me that error http://puu.sh/i4DQn/4da5dd5cc6.png.And i can't change my json structure because this api was currently working with all service and I am making new product for iOS.;( – Thiha Aung May 29 '15 at 10:10
  • If you cant change the json then my answer wont work, you'll have to make a pattern for your keys, As far as Ive seen every key looks like this x_xxx the left x is incremented in your json: 0, 1_xxx, 2_xx ... – Arbitur May 29 '15 at 10:54
  • Can i ask you something?I found the other way that i used Alamofire and SwiftyJSON and change the response to string.So the question is can i turn that json that i show in figure http://puu.sh/i4FAN/a869132769.png to NSDicitionary? – Thiha Aung May 29 '15 at 11:03
  • I havent used any third party json parser so im not sure. But alamofire maybe can do what you request. – Arbitur May 29 '15 at 11:40
  • Thank you Arbitur,Now I told my boss to change the json format like you said.Thanks for your help.Really appreciated – Thiha Aung Jun 15 '15 at 04:18