我如何...将Json Data填入下拉列表 [英] How do I...fill Json Data into dropdown list

查看:74
本文介绍了我如何...将Json Data填入下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的Json数据

 [{CountryId:India,CountryName:India},{CountryId:Pakisthan ,CountryName:Pakisthan},{CountryId:USA,CountryName:USA}] 







如何将此数据绑定到下拉列表

解决方案

步骤1 :声明自定义类

  public   class 国家
{
public string CountryId { get < /跨度>; set ; }
public string CountryName { get ; set ; }
}



第2步:反序列化Json数据并将其绑定到Dropdown

  string  jsonData =  < /跨度>;  //  将JSON数据分配给变量 

// 创建JavascriptSerializer对象
JavaScriptSerializer jsSerializer = JavaScriptSerializer( );
列表< country> countryList = jsSerializer.Deserialize< list>< country>>(jsonData);

// 将数据绑定到下拉列表
DropDownList1.DataSource = countryList;
DropDownList1.DataTextField = CountryName;
DropDownList1.DataValueField = CountryId;
DropDownList1.DataBind(); `
< / country > < / list > < / country >



这里 DropDownList1 是Dropdownlist的ID属性值

i have Json Data in Below Format

[{"CountryId":"India","CountryName":"India"},{"CountryId":"Pakisthan","CountryName":"Pakisthan"},{"CountryId":"USA","CountryName":"USA"}]




How can i bind this data into dropdown list

解决方案

Step 1: Declare a custom class

public class Country
{
    public string CountryId { get; set; }
    public string CountryName { get; set; }
}


Step 2: Deserialize the Json data and bind it to Dropdown

string jsonData = ""; // Assign JSON data to variable

// Create JavascriptSerializer object
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<country> countryList = jsSerializer.Deserialize<list><country>>(jsonData);

// Binding data to dropdownlist
DropDownList1.DataSource = countryList;
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "CountryId";
DropDownList1.DataBind(); `
</country></list></country>


Here DropDownList1 is value of ID attribute of Dropdownlist


这篇关于我如何...将Json Data填入下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆