请问一下用.NET怎么将string转换成Json格式?
我现在要将ListBox里面所有的value和Text转换成json格式?
同时怎么将Json转换成string而绑定ListBox
希望能够贴出代码。
[{"ID":"05","Name":"NQ022005"},{"ID":"09","Name":"NQ022009"},{"ID":"01","Name":"NQ021001"}]"
我现在要将ListBox里面所有的value和Text转换成json格式?
同时怎么将Json转换成string而绑定ListBox
希望能够贴出代码。
[{"ID":"05","Name":"NQ022005"},{"ID":"09","Name":"NQ022009"},{"ID":"01","Name":"NQ021001"}]"
if(string.IsNullOrEmpty(jsonStr))
throw new Exception("课程未配置学习进度计算权值");
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonStr));
Rank = (ProgressRank)jsonConvert.ReadObject(ms);
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
前台
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body onload="bodyLoad()">
<select id="select1" runat="server"></select>
</body>
<script language="javascript" type="text/javascript">
var selectItem = <%=this.SelectJson %>;
function bodyLoad(){
for(i=0;i<selectItem.length;i++)
{
var option = document.createElement('option');
option.value = selectItem[i].id;
option.innerHTML = selectItem[i].name;
document.getElementById("select1").appendChild(option);
}
}
</script>
</html>
后台protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Rows.Add(new object[] { "1", "张三" });
dt.Rows.Add(new object[] { "2", "李四" });
dt.Rows.Add(new object[] { "3", "赵五" }); this.SelectJson = "["; bool isNotFarst = false; string strFormat = "{{id:'{0}',name:'{1}'}}";
foreach (DataRow dr in dt.Rows)
{
if (isNotFarst)
{
this.SelectJson = ",";
}
isNotFarst = true;
this.SelectJson = string.Format(strFormat, dr["id"], dr["name"]);
} this.SelectJson = "]";
} /// <summary>
/// 页面显示的select 的 item 的json字符串
/// </summary>
private System.Text.StringBuilder _selectJson = new System.Text.StringBuilder(); protected string SelectJson
{
get { return this._selectJson.ToString(); }
set { this._selectJson.Append(value); }
}