服务器返回Json格式的响应内容经常是以 String (txt) 形式返回给客户端。客户端需要把 文本形式的内容还原为Json格式以进一步做处理(如,取得返回内容的一个值作为下个请求的一个输入)。这就要用到 一个函数 evel()。 具体做法如下:
 
Parsing the JSON Response
1. In the InitAgenda() function in the Agenda, define the global variable values of the SaveSource property as follows: 
function InitAgenda() 

    wlGlobals.SaveSource = true 

This instructs WebLOAD to store the complete HTML source code downloaded in the document.wlSource object.
 
2. Drag the JavaScript object Building Block from the Toolbox to the Agenda Tree. In  the Building Block, add a function that receives the document.wlSource object and manipulates it to retrieve the statistics. The script is as follows: 
function evalResponse ( source) {
    json_response = eval("(" + source + ")")
}
 
3. Call the evalResponse function to parse the response contents:
evalResponse(document.wlSource);
 
---------------------------------------具体做法是:-------------------------------
 
1. 把解析JSON 的函数 提取出来 放在一个单独的parseJSON.js文件里,以便重复利用:
function parseJSON(source){
    jsonResponse = eval("(" + source + ")");
    return jsonResponse;
}
 
2. 在脚本里引进 parseJSON.js 文件:
function InitAgenda()
{
    wlGlobals.SaveSource = true; 
    IncludeFile("FunctionLib\\parseJSON.js");  //IncludeFile 引进的文件是相对脚本wlp文件的位置;即这里的
                                                                  Function文件夹和wlp文件是放在同一个根目录下的
}
 
3. 直接引用parseJSON 文件里的函数:
 
var loginResponse = document.wlSource;
var loginObj = parseJSON(loginResponse);
var advisorId = loginObj.AdvisorId;
 
当然也可以不需要引进外部文件,而是直接把 eval()函数放在脚本里
 
注:JSON 节点的读取,如果是{}结构则 直接 a.b, 如果是[], 则 c[index].d 
 
如下面是一个返回的总结构:
{
"ReturnVsBenchmark": {},
"RiskReward": {},
"Portfolios": {},
"Information": {}
}

展开上面Portfolios 的层次结构如下:

"Portfolios": {
"Id": "Portfolios",
"Label": "Portfolios",
"Columns": [],
"Sections": [
{},
{},
{
"Rows": [
{
"Id": "95646f0f-879d-448b-8bf3-c9bcb15bedd0",
"Status": "Actual",
"OneYear": 0.13786034,
"ThreeYear": 0.14883958,
"FiveYear": 0.13716583,
"TenYear": 0.11452207,
"TrailingAsOfDate": "2014-10-31",
"MarketValue": 124809.13719108,
"MarketValueCurrencyCode": "USD",
"ThreeYearStdDev": 10.08655991,
"ThreeYearMean": 0.14883958,
"RiskRewardAsOfDate": "2014-10-31",
"AccountName": "Portfolio 1",
"YTD": 0.09898687,
"ReturnDate": "2014-10-31"
},
{},
{}
]
},
{},
{}
]
},

要取得 "Id"的值"95646f0f-879d-448b-8bf3-c9bcb15bedd0"  ,则读取的脚本如下:

var clientResponse = document.wlSource;  // 把 返回的内容存放一个变量中
var clientObj = eval( "(" + clientResponse + ")" );  // 把返回的txt 格式的内容转化为JSON 格式
var portfolioId1 = clientObj.Portfolios.Sections[2].Rows[0].Id; //Sections[2]取得是Rows 这个对象,在Sections[]里, 每个{}对象都是Sections 对象数组的一个元素

WebLoad 解析服务器返回的JSON格式内容的更多相关文章

  1. WebLoad 解析服务器返回的XML格式内容

    Parsing the XML Response get the root node:  var rootNode = document.wlXmls[0].XMLDocument.documentE ...

  2. javascript解析从服务器返回的json格式数据

    在javascript中我们可以将服务器返回的json格式数据转换成json格式进行使用,如下: 1.服务器返回的json格式数据: 通过response.responseText获得: " ...

  3. ajaxFileUpload上传文件成功后却无法解析服务器返回的json数据

    可能是应该返回内容带了标签,过滤下 var index=data.indexOf("<"); if (index!=-1){ data=data.substring(0,in ...

  4. 在C#中通过使用Newtonsoft.Json库来解析百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据

    百度地图地理编码(GeoCoder)服务接口返回的Json格式的数据,如下所示: http://api.map.baidu.com/geocoding/v3/?address=**省**市**区**路 ...

  5. JS前端取得并解析后台服务器返回的JSON数据的方法

    摘要:主要介绍:使用eval函数解析JSON数据:$.getJSON()方法获得服务器返回的JSON数据 JavaScript eval() 函数 eval(string) 函数可计算某个字符串,并执 ...

  6. [转]android中解析后台返回的json字符串

    普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname&qu ...

  7. [Swift通天遁地]四、网络和线程-(11)将服务器返回的JSON映射为实例对象

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. $.ajax返回的JSON格式的数据后无法执行success的解决方法

    近段时间做项目,在项目使用了ajax技术,遇到了一个奇怪的问题:"$.ajax返回的JSON格式的数据无法执行success",代码是这样写的: 1 $.ajax({ 2 .. 3 ...

  9. 接口返回数据Json格式处理

    有这样一个页面 , 用来显示用户的账户记录数据,并且需要显示每个月的 收入 支出合计 ,在分页的时候涉及到一些问题,需要对返回的Json格式做处理,处理起来比较麻烦,后端返回的Json数据格式形式如下 ...

随机推荐

  1. 229 Majority Element II 求众数 II

    给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 你的算法应该在O(1)空间中以线性时间运行. 详见:https://leetcode.com/problems/major ...

  2. 转】用Nodejs连接MySQL

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! 用Nodejs连接MySQL 从零开始node ...

  3. A8通用权限框架

  4. java自动包装与解包

    关于java的自动包装机制想必大家都用过吧,一般这些机制都用于在往容器中存储基本类型数据的时候,因为容器中不允许存在基本数据类型,所以就会调用自动包装机制,将基本数据类型转换为对象,将基本数据保存在对 ...

  5. sublime text3安装Package Control

    转自:https://www.cnblogs.com/lq147760524/p/8202521.html 一.下载Sublime3 https://www.sublimetext.com/3 二.安 ...

  6. 5 Transforms 转移 笔记

    5 Transforms 转移 笔记   Transforms    Unfortunately, no one can be told what the Matrix is. You have to ...

  7. RegisterClientScriptBlock和RegisterStartupScript的区别

    RegisterClientScriptBlock在 Page 对象的 元素的开始标记后立即发出客户端脚本,RegisterStartupScript则是在Page 对象的 元素的结束标记之前发出该脚 ...

  8. Node.js——重定向

  9. elasticsearch学习笔记-倒排索引以及中文分词

    我们使用数据库的时候,如果查询条件太复杂,则会涉及到很多问题 1.无法维护,各种嵌套查询,各种复杂的查询,想要优化都无从下手 2.效率低下,一般语句复杂了之后,比如使用or,like %,,%查询之后 ...

  10. 【4412开发板使用经验分享】迅为4412开发板I2C驱动问题

    本文转自迅为论坛:bbs.topeetboard.com 我想写DS3231 的驱动 但是读回的数据老是-6 硬件: 我I2C设备连接的这几个GPIO,看了2.5的手册,接口应该是链接正确的 软件 分 ...