首页 > 代码库 > JSON字符串转JSON对象

JSON字符串转JSON对象

<script type="text/javascript">
    var _notifyTRTemplate = "<tr><td><a href=http://www.mamicode.com/‘./ZYYatHenan/Login_Return_XML.aspx?id={id}‘ target=‘_blank‘>{title}{time}";
    $(function () {
        $.ajax({
            type: "GET",
            url: "ZYYatHenan/login.aspx",
            data: "num=5",
            success: function (msg) {
                var _data = http://www.mamicode.com/eval("(" + msg + ")");
                var _appentHtml = "";
                //_data = http://www.mamicode.com/(new Function("", "return " + msg))();
                for (var i = 0; i < _data.length; i++) {
                    _appentHtml += _notifyTRTemplate.replace("{id}", _data[i].ID).replace("{title}", _data[i].Title).replace("{time}", _data[i].PublishTime);
                }
                $("#notifyContent").append(_appentHtml);
            }
        });

    });
</script>
DataSet ds = DataAccessor.ExecuteDataSet(sql);
string returnJson = "";
JavaScriptSerializer jss = new JavaScriptSerializer();

if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
    StringBuilder sb = new StringBuilder();

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        sb.Append("," + jss.Serialize(new NotifyItem(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(),
            dr[3].ToString(), dr[4].ToString(), dr[5].ToString())));
    }

    returnJson = "[" + sb.ToString().Substring(1) + "]";
}

Response.Write(returnJson);
Response.End();

    public class NotifyItem
    {
        public string publishTime = "";

        public string ID { get; set; }
        public string Title { get; set; }
        public string Type { get; set; }
        public string PublishTime { 
            get { return publishTime; } 
            set { publishTime = value.Trim(); } }
        public string PublishDept { get; set; }
        public string CreatePerson { get; set; }

        public NotifyItem(string id, string title, string type, string publisTime, 
            string publishDept, string createPerson)
        {
            ID = id;
            Title = title;
            Type = type;
            PublishTime = publisTime;
            PublishDept = publishDept;
            CreatePerson = createPerson;
        }
    };





JSON字符串转JSON对象