-4
hi its my json string.

{"state":true,"datas":{"5577":"Adana","5548":"Adapazari","5568":"Afyon","5572":"Ahmetli","5638":"Ak \u00e7aabat","5653":"Aksaray","5521":"Akseki","5564":"Ak\u015fehir","5527":"Alanya","5729":"Alibeyk\u00f6y","5536 ":"Anamur","5510":"安卡拉","5535":"安塔利亚","5563":"Argithani","5713":"Ata\u015fehir","5727":"Ata\u015fehir ( 后宫)","5524":"Avsallar","5606":"Aydin","5538":"Aydincik","5683":"Balikesir","5569":"Banaz","5719":"Bandirma","5640":"Be\u015fikd\u00fcz\u00fc","5605":"Bey\u015fehir","5633":"Bilecik","5743":"博德鲁姆","5550 ":"Bolu","5584":"Bolvadin","5590":"Bor","5581":"Bozh\u00fcy\u00fck","5537":"Bozyazi","5645":"Blancak", "5579":"布尔萨","5511":"Cihanbeyli","5585":"\u00c7akmak","5614":"\u00c7ar\u015famba","5567":"\u00c7ay","5737":" \u00c7erkezk\u00f6y","5658":"\u00c7ifteler","5533":"\u00c7olakli","5736":"\u00c7orlu","5609":"\u00c7orum","5708":"Dazkiri","5646":"Delice","5543":"Demirta\u015f","5596":"Denizli","5742":"Didim","5597":"Dinar"," 5671":"D\u00f6rtyol","5712":"D\u00fczi\u00e7i","5599":"E\u011firdir","5648":"Elmada\u011f","5583":"Emirda\u011f" ,"5555":"Erdemli","5558":"Ere\u011fli","5560":"Ermenek","5670":"Erzin","5728":"Esenler","5582":"Eski\ u015fehir","5644":"Espiye","5652":"E\u015fmekaya","5641":"Eynesil","5617":"Fatsa","5517":"Federasyon","5709":"加济安泰普","5542":"Gazipa\u015fa","5546":"Gebze","5600":"Gelendost","5691":"Gemlik","5619":"Giresun"," 5665":"G\u00f6lba\u015fi","5642":"G\u00f6rele","5539":"G\u00fclnar","5738":"G\u00fcneyyurt","5622":"后宫"," 5680":"Hatay","5611":"Havza","5601":"H\u00fcy\u00fckyolu","5562":"Ilgin","5598":"Isparta","5580":"\u0130neg \u00f6l","5673":"\u0130skenderun","5545":"\u0130stanbul","5576":"\u0130zmir","5547":"\u0130zmit","5528":"K.karabekir","5731":"K.mara\u015f","5720":"Karacabey","5529":"Karaman","5559":"Karapinar","5612":"Kavak"," 5549":"Kayna\u015fli","5593":"Kayseri","5692":"Kemalpa\u015fa","5664":"Kemer","5602":"Kireli","5607":"Kirikkale" ,"5554":"Kizkalesi","5525":"Konakli","5508":"Konya","5573":"Kula","5514":"Kulu","5515":"Kulu makasi", "5701":"Ku\u015fadasi","5632":"K\u00fctahya","5520":"Maden","5544":"Mahmutlar","5523":"Manavgat","5557":"Mersin","5610":"Merzifon","5760":"Milas","5761":"Mu\u011fla","5522":"Murti\u00e7i","5540" :"Mut","5594":"Nazilli","5654":"Nev\u015fehir","5591":"Ni\u011fde","5710":"Nurda\u011fi","5618":"Ordu" ,"5688":"Orhangazi","5711":"Osmaniye","5634":"Pamukova","5672":"Payas","5588":"Pozanti","5574":"Salihli"," 5613":"Samsun","5631":"Sandikli","5703":"Sel\u00e7uk","5534":"Serik","5541":"Sertavul","5518":"Seydi\u015fehir","5531":"Side","5551":"Silifke","5744":"S\u00f6ke","5565":"Sultanda\u011fi","5651":" Sultanhani","5608":"Sungurlu","5552":"Susano\u011flu","5689":"Susurluk","5621":"\u015e.karaa\u011fa\u00e7","5578":"Tarsus ","5513":"Tav\u015fan\u00e7ali","5739":"Teke​​li","5615":"Terme","5643":"Tirebolu","5676":"Topbo\u011fazi","5620 ":"特拉布宗","5575":"Turgutlu","5759":"Turgutreis","5587":"Uluki\u015fla","5570":"U\u015fak","5616":"\u00dcnye","5639":"Vakfikebir","5687":"Yalova","5762":"Yata\u011fan","5512":"Yeniceoba makasi","5592":"Ye\ u015filhisar","5589":"曾根"}}

我如何添加项目,下拉列表

foreach (XXXX in YYYY)
            {
                DropDownList1.Items.Add(new ListItem("Adana", "5577"));
            }
4

3 回答 3

1

使用Json.Net

var response =  JsonConvert.DeserializeObject<Response>(json);
foreach(var item in response.datas)
{
    DropDownList1.Items.Add(new ListItem(item.Value, item.Key));
}

public class Response
{
    public bool state;
    public Dictionary<string, string> datas;
}
于 2012-08-16T19:10:35.657 回答
0

您必须首先将 json 反序列化为字典 http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C ,然后在字典中迭代以填充您的 DropDownList1

于 2012-08-16T19:08:17.053 回答
0

这应该有助于:

http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

反序列化 json 数据,然后对其进行迭代。

于 2012-08-16T19:05:44.607 回答