JSON in JavaScript Asp.net

首先官方网站  http://www.json.org/js.html

下载 json2.js 到Github 中下载 https://github.com/douglascrockford/JSON-js

后台定义并生成json 后返回前台


在这里我们在后台做一个简单的返回(在AbnormalCustomerList.ashx.cs中)

string stbList = "{"Rows":[";
            stbList = stbList + "{ "id": 1, "name": "林三", "sex": "男", "birthday": "1989/01/12"},";
            stbList = stbList + "{ "id": 2, "name": "张武", "sex": "男", "birthday": "1986/11/24"}";
            stbList = stbList +"],";
            stbList = stbList + ""Count":[{"total":2}]";//用来记录一共返回了几条数据记录
            stbList = stbList +"}";

 context.Response.Write(stbList.ToString());

前台引用

var varReceiver = JSON.parse(e.result);
         //alert(varReceiver.Rows[0].name)
         for (var i = 0; i < varReceiver.Count[0].total; i++)
         {
             alert(varReceiver.Rows[i].name)
         }
        

在alert的时候就会依次Popup得到 林三 张武

另外:JSON.parse()和JSON.stringify()的用法

http://blog.csdn.net/wangxiaohu__/article/details/7254598

上述做法,对stbList的生成有很严格的要求,如果有一个字符拼凑出错都会导致前台引用的失败。

原文地址:https://www.cnblogs.com/xp1056/p/5287187.html