请问一下用.NET怎么将string转换成Json格式?

sclsuccess2010 2010-05-24 05:00:00
请问一下用.NET怎么将string转换成Json格式?
我现在要将ListBox里面所有的value和Text转换成json格式?
同时怎么将Json转换成string而绑定ListBox
希望能够贴出代码。
[{"ID":"05","Name":"NQ022005"},{"ID":"09","Name":"NQ022009"},{"ID":"01","Name":"NQ021001"}]"
...全文
844 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yajun_snow 2010-05-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 woainilei 的回复:]

楼上的,可以在js 中写<%%>的吗?
[/Quote]
当然可以
woainilei 2010-05-24
  • 打赏
  • 举报
回复
楼上的,可以在js 中写<%%>的吗?
  • 打赏
  • 举报
回复
我这里有一个前两天写的绑定下拉列表的例子。你看下吧。
前台
<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); }
}

staywithc 2010-05-24
  • 打赏
  • 举报
回复
命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
staywithc 2010-05-24
  • 打赏
  • 举报
回复
一般是把json转换成实体类,然后你再操作不就方便了吗

DataContractJsonSerializer jsonConvert = new DataContractJsonSerializer(typeof(ProgressRank));
if(string.IsNullOrEmpty(jsonStr))
throw new Exception("课程未配置学习进度计算权值");
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonStr));
Rank = (ProgressRank)jsonConvert.ReadObject(ms);
01040201 2010-05-24
  • 打赏
  • 举报
回复
.net里没必要转成json吧,如果要给js用的话可以转成json,如果直接用的话就用分隔符分开就好了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧