首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么读取JSON

2012-03-19 
如何读取JSON求一完整的解决方案[解决办法]HTML code采用第三方组件Jayrock 和 Jayrock.Json首先引入命名

如何读取JSON
求一完整的解决方案

[解决办法]

HTML code
采用第三方组件Jayrock 和 Jayrock.Json首先引入命名空间using Jayrock.Json;其次,创建 JsonObject 对象,步骤如下:string strJsonText = @"{"cacheCount":1,"count":"34","slice":"5, 5","list":[1001598,1001601,1001605,1001609,1001612],"page":1,"error":200}";JsonReader reader = new JsonTextReader(new StringReader(strJsonText));JsonObject jsonObj = new JsonObject();jsonObj.Import(reader);这样,就将一个文本的JSon数据转变成一个对象,如果要获取 count 的值,则可以这样string count = jsonObj["count"].ToString();但是有个问题,list 是一个数组,该如何获取呢?不用急,Jayrock已经为我们准备好了,来看using (JsonTextReader textReader = new JsonTextReader(new StringReader(jsonObj["list"].ToString()))){                    while (textReader.Read())                    {                        if (!string.IsNullOrEmpty(textReader.Text))                        {                            Response.Write(textReader.Text);                        }                    }}将数组的内容再赋予一个JsonTextReader对象 ,利用其Read方法进行逐行读取就OK了当然,你也可以使用 JsonArray 对象,这里就不再叙述了
[解决办法]
可以用ajax读取Json数据,或者在EXTJS中读取的json数据
[解决办法]
直接JS
加eval()转换就就可以了

JS要好好学习的啊

[解决办法]
HTML code
用Ajax读取JSON格式的数据,也需要先用XMLHttpRequest对象的responseText属性读取,然后再用Function构造返回JSON对象的方法,能过方法创建JSON对象。代码如下:1、Client - helloworld.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>Ajax Hello World</title><script type="text/javascript">var xmlHttp;function createXMLHttpRequest(){     if(window.ActiveXObject){         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");     }     else if(window.XMLHttpRequest){'         xmlHttp = new XMLHttpRequest();     }}function startRequest(){     createXMLHttpRequest();:     try{         xmlHttp.onreadystatechange = handleStateChange;         xmlHttp.open("GET", "data.txt", true);         xmlHttp.send(null);      }catch(exception){         alert("您要访问的资源不存在!");     }} function handleStateChange(){     if(xmlHttp.readyState == 4){              if (xmlHttp.status == 200 || xmlHttp.status == 0){             // 取得返回字符串             var resp = xmlHttp.responseText;             // 构造返回JSON对象的方法             var func = new Function("return "+resp);             // 得到JSON对象             var json = func( );             // 显示返回结果- \+             alert("JSON's value: " + json.info + "(" + json.version + "v)");         };      })}/ </script></head><body>    <div>        <input type="button" value="return ajax JSON's value"+                 onclick="startRequest();" /></div></body>. </html>2、Server - data.txt{: ainfo: "hello world!",    version: "2.0"}
[解决办法]
用Ajax读取JSON格式的数据,也需要先用XMLHttpRequest对象的responseText属性读取,然后再用Function构造返回JSON对象的方法,能过方法创建JSON对象。代码如下:
1、Client - helloworld.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Hello World</title>
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){'
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();:
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.txt", true);
xmlHttp.send(null); 
}catch(exception){
alert("您要访问的资源不存在!");
}
}
 function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得返回字符串
var resp = xmlHttp.responseText;
// 构造返回JSON对象的方法
var func = new Function("return "+resp);
// 得到JSON对象
var json = func( );
// 显示返回结果- \+
alert("JSON's value: " + json.info + "(" + json.version + "v)");
}; 
})
}/ 
</script>
</head>
<body>
<div>
<input type="button" value="return ajax JSON's value"+
onclick="startRequest();" />
</div>
</body>. 
</html>
2、Server - data.txt
{
info: "hello world!",
version: "2.0"
}

热点排行