接收JSON对象

本节示例可以参考示例文件"CaseStudy\WebViewer\发送消息.Table"的窗口"接收JSON对象"。

我们不仅可以向前端函数传递JSON对象,还可以接收从前端函数返回的JSON对象。

示例

1、窗口的AfterLoad事件代码:

'''Async
Dim
wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
Await
wv.EnsureCoreWebView2Async(Nothing)
Dim html As String = <![CDATA[
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>JSON
参数演示 - 返回JSON对象</title>
    <style>
        body { font-family: 'Microsoft YaHei'; padding: 20px; }
        .result { background: #f0f0f0; padding: 15px; border-radius: 5px; margin-top: 20px; font-family: monospace; }
    </style>
</head>
<body>
    <h2>JSON
参数传递演示</h2>
    <p>
前端函数接收JSON参数,返回JSON对象</p>

    <div id="result" class="result">等待调用...</div>

    <script>
        //
函数接收JSON参数,返回JSON对象
        function calculateYearSalary(employee) {
            //
计算年薪(月薪 * 12
            let yearSalary = employee.salary * 12;           

            // 构建返回的JSON对象
            let result = {
                name: employee.name,
                monthSalary: employee.salary,
                yearSalary: yearSalary,
                title: employee.title || '
普通员工',
                message: `
你好,${employee.name},你的年薪是 ${yearSalary.toFixed(2)} `
            };
           
            //
显示在页面上
            document.getElementById('result').innerText = JSON.stringify(result, null, 2);         

            // 返回JSON对象
            return result;
        }       
    </script>
</body>
</html>
]]>
.Value
wv.NavigateToString(html)

2、Foxtable调用这个函数的参考代码为:

'''Async
Dim wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
'
这里用ExpandoObject,你用JObject匿名类型或字典也行
Dim
emp = New ExpandoObject()
emp.name =
"张三"
emp.age = 30
emp.salary = 8500.50
emp.title =
"工程师"
Dim
jsonEmp = JsonConvert.SerializeObject(emp) ' 将ExpandoObject序列化成json字符串
Dim
js = CExp("calculateYearSalary({0});", jsonEmp)
Dim
resultJson = Await wv.ExecuteScriptAsync(js)
Dim result = JsonConvert.DeserializeObject(resultJson)

MessageBoxA.Show(
"姓名:" & result("name") & ",年薪:" & result("yearSalary") & "", "接收JSON对象")

提示:

上面将收到的JSON数据转换为JObject:

Dim result = JsonConvert.DeserializeObject(resultJson)

如果需要转换为
ExpandoObject,只需将上面这样代码改为:

Dim
result = JsonConvert.DeserializeObject(Of ExpandoObject)(resultJson)

因为历史原因,本文档多数时候使用的是JObject,我个人更倾向使用ExpandoObject可以像访问属性一样访问收到的数据,例如:

'''Async
Dim wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
'这里用ExpandoObject,你用JObject、 匿名类型或字典也行
Dim emp  = New ExpandoObject()
emp.name =
"张三"
emp.age = 30
emp.salary = 8500.50
emp.title =
"工程师"
Dim jsonEmp = JsonConvert.SerializeObject(emp) ' 将ExpandoObject序列化成json字符串
Dim js = CExp("calculateYearSalary({0});", jsonEmp)
Dim
resultJson = Await wv.ExecuteScriptAsync(js)
Dim
result = JsonConvert.DeserializeObject(Of ExpandoObject)(resultJson) '将收到的JSON字符串反序列化成ExpandoObject
MessageBoxA.Show(
"姓名:" & result.name & ",年薪:" & result.yearSalary & "", "接收JSON对象")

需要的话,也可以将收到的JSON对象转换为字典,例如:

Dim result= JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(resultJson)


本页地址:http://www.foxtable.com/webhelp/topics/6373.htm