验证/清除JSON数据(Validate/Scrub JSON Data)

编程入门 行业动态 更新时间:2024-10-25 14:23:26
验证/清除JSON数据(Validate/Scrub JSON Data)

我正在进行Web服务调用并接收JSON格式的响应。 收到回复后,我想在页面中显示数据。 在我在页面上显示这些数据之前,我需要执行检查以确保定义了某些元素,如果没有给它们默认值(例如下面的例子)。

var scoreSummary = JSON.parse(response).summary; var gameStatus = scoreSummary.gameStatus ? scoreSummary.gameStatus : 'pre'; var homeRanking = scoreSummary.homeRank ? scoreSummary.homeRank : ''; var awayRanking = scoreSummary.awayRank ? scoreSummary.awayRank : ''

现在我有几个页面,我可以调用这个相同的Web服务,我不想复制所有检查,并设置默认值。 有没有办法可以集中这些检查,以便我只将它们放在一个地方,而不是分散在几个页面中,每个页面都进行相同的服务调用?

谢谢

I am making a web service call and receiving a JSON formatted response. After I receive the response, I would like to display the data in a page. Before I display this data on the page I need to perform checks to make sure that certain elements are defined, and if not give them default values (example below).

var scoreSummary = JSON.parse(response).summary; var gameStatus = scoreSummary.gameStatus ? scoreSummary.gameStatus : 'pre'; var homeRanking = scoreSummary.homeRank ? scoreSummary.homeRank : ''; var awayRanking = scoreSummary.awayRank ? scoreSummary.awayRank : ''

Now I have several pages where I can call this same web service and I do not want to duplicate all the checks, and setting of default values. Is there a way that I can centralize these checks so that I only have them in one place instead of scattered throughout several pages that each make the same service call?

Thanks

最满意答案

就像一个函数调用?

function setDefaultValues(jsonResponse) { var scoreSummary = JSON.parse(jsonResponse).summary; scoreSummary.gameStatus = scoreSummary.gameStatus || 'pre'; scoreSummary.homeRank = scoreSummary.homeRank || ''; scoreSummary.awayRank = scoreSummary.awayRank || ''; return scoreSummary; }

Like, a function call?

function setDefaultValues(jsonResponse) { var scoreSummary = JSON.parse(jsonResponse).summary; scoreSummary.gameStatus = scoreSummary.gameStatus || 'pre'; scoreSummary.homeRank = scoreSummary.homeRank || ''; scoreSummary.awayRank = scoreSummary.awayRank || ''; return scoreSummary; }

更多推荐

本文发布于:2023-07-31 23:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   JSON   Validate   Data   Scrub

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!