如何在NSDictionary中获取与json相同的数据

时间:2015-05-29 07:27:42

标签: json swift nsdictionary

我在使用json生成的NSDictionary时遇到问题。我不知道我的NSDictionary有什么问题,这意味着我在NSDictionary中没有得到像json这样的排序键当我试图输出它时。请帮助

这是我在intellij IDEA REST工具中测试过的json

{"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"]}

正如您所看到的那些按键带有0,1_190,2_69,3_61,4_86等,其值为“All Venues,AEP Gifu,ARAI Bayside”(按字母顺序排列为A,B,C,D) ...)“

但是当我使用NDSictionary添加到NSJSONSerialization后创建json的输出时,它看起来像那样

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"
);
})

如你所见,它不像我之前的json。我不知道为什么以及如何获得这样的数据。如果我想得到像json那样排序的相同数据,我该怎么做?请帮助....我已经坚持这个问题两天了。我还没有答案。我尝试了所有的排序方法,我的键和值仍然无效。希望这段代码可以帮助。

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()
}

[更新]我尝试输出的NSDictionary的值在快速期间按升序排序从this开始。但是,我希望“所有场地”开始而不是“AEP Gifu”。如何做我需要做什么?

1 个答案:

答案 0 :(得分:1)

如果顺序很重要,则不能依赖NSDictionary,因为它的键是任意的。

要解决此排序问题,您可能希望使用数组。

所以将你的json结构改为:

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

转换后,您将拥有[String:[[String:[String]]]]结构。

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