日期:2014-05-18  浏览次数:20330 次

如何读取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{: a
info: "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(