服务器返回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. Xors on Segments Codeforces - 620F

    http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...

  2. Service官方教程(4)两种Service的生命周期函数

    Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...

  3. Environment中有大量访问目录的函数

    public class Environment { /** * Return root of the "system" partition holding the core An ...

  4. Android Dialogs(5)[正常显示dlg,将Fragment显示为dialog,将Aty显示为dlg,嵌入],关闭Dialog

    Showing a Dialog When you want to show your dialog, create an instance of your DialogFragment and ca ...

  5. Xcode配置SVN详细步骤

    转载:http://blog.csdn.net/weiqubo/article/details/8288635   Xcode 默认自带Git 与 SVN,我们本篇介绍SVN的详细配置步骤如下: 1. ...

  6. Hadoop集群搭建及MapReduce应用

    一.Hadoop集群的搭建与配置 1.节点准备 集群规划: 主机名 IP 安装的软件 运行的进程 weekend 01 192.168.1.60 jdk.hadoop NameNode.DFSZKFa ...

  7. 45 个非常有用的 Oracle 日期查询语句

    日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...

  8. 聊聊mq的使用场景

    mq的作用 通过异步方式对系统解耦 增加系统的并发处理能力 通过异步方式对系统解耦 以用户注册为例,一般情况下: 分下一下,上面过程存在的一些问题: 注册过程会调用4个服务(注册服务.邮件服务.短信服 ...

  9. java自动包装与解包

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

  10. 学习笔记 第九章 使用CSS美化表格

    第9章  使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...