开发者社区> 问答> 正文

怎么判断从网页读取的数据是否刷新了?:报错

我需要从一个网址https://bter.com/api/1/ticker/btc_cny中读取数据
数据时json格式的,类似如下
{"result":"true","last":77.9,"high":78.95,"low":75.04,"avg":76.61,"sell":77.9,"buy":77.8,"vol_ltc":6000.8096,"vol_cny":459746.35}
数据每10秒更新,但是数据不一定会变,我怎么判断数据是否有刷新?
因为读取数据我需要存储起来,变化的数据才会存起来,不变的数据就不会存储起来。

展开
收起
kun坤 2020-06-06 21:47:46 467 0
1 条回答
写回答
取消 提交回答
  • 如果每次读取的数据很多的话类似:

    {"result":"true","data":[{"date":"1398348450","price":3088,"amount":0.8039,"tid":"7180798","type":"sell"},{"date":"1398348517","price":3085,"amount":0.8253,"tid":"7180825","type":"sell"},{"date":"1398348533","price":3084.19,"amount":0.0309,"tid":"7180828","type":"sell"},{"date":"1398348538","price":3084.19,"amount":0.039,"tid":"7180831","type":"sell"},{"date":"1398348603","price":3086.17,"amount":0.0176,"tid":"7180841","type":"sell"},{"date":"1398348697","price":3088,"amount":0.1631,"tid":"7180863","type":"buy"},{"date":"1398348702","price":3085.4,"amount":0.0601,"tid":"7180868","type":"sell"},{"date":"1398348707","price":3085.4,"amount":0.0697,"tid":"7180871","type":"sell"},{"date":"1398348712","price":3085.4,"amount":0.0526,"tid":"7180873","type":"sell"},{"date":"1398348807","price":3085.41,"amount":0.6088,"tid":"7180911","type":"sell"},{"date":"1398348818","price":3085.41,"amount":0.002,"tid":"7180913","type":"sell"},{"date":"1398348829","price":3088,"amount":0.0063,"tid":"7180916","type":"buy"},{"date":"1398348839","price":3088.1,"amount":0.0501,"tid":"7180923","type":"buy"},{"date":"1398348930","price":3086.41,"amount":0.1079,"tid":"7180943","type":"sell"},{"date":"1398348962","price":3089.2,"amount":0.0185,"tid":"7180948","type":"sell"},{"date":"1398348972","price":3090,"amount":0.031,"tid":"7180951","type":"buy"},{"date":"1398348983","price":3090,"amount":0.051,"tid":"7180959","type":"buy"},{"date":"1398348993","price":3090,"amount":0.0027,"tid":"7180961","type":"buy"},{"date":"1398348993","price":3091,"amount":0.0466,"tid":"7180963","type":"buy"}],"elapsed":"0.752ms"}



    每次改变的也许只是其中的一笔数据或者两笔数据,难不成还要全部对比所有的数据?这不是很麻烦?

    ######

    MD5下字符串  每次比较MD5后的结果

    ######为什么要MD5呢?我直接比较字符串不行么?######+1######

    引用来自“huaye2007”的评论

    MD5下字符串  每次比较MD5后的结果

    +1######

    从页面抓取过来应该是序列化的json吧,直接对比字符串不行嘛? 如果是对象格式,用JSON.stringify序列化一下再对比。

    顺便问下楼主,如果data数组里面两个元素交换了顺序,算不算是数据变化呢?如果这种不算数据变化,那任何对比字符串的方式都白瞎了

    ######这里面的数据是会更新的,会返回最新的几十笔交易数据,若我每10秒读取一次,数据有可能全部刷新,有可能部分跟新,还有可能不变。。但是我需要每次读取将数据存储起来,存储起来的是新数据,以前存过的便不用存了。。但是每一个判断准则,所以好头疼。######

    做两种查询

    一种结果里面带个"update time",返回个时间戳

    一种是这种查询整套数据的

    从流量和响应来说都好

    ######回复 @登峰 : 两种查询.一种是查询数据最新更新时间,如果后端前端一致就不必发送获取更新数据的请求. 否则如果有更新则从另外一种查询返回更新过的整套数据, 不是返回所有数据.######查询整套数据?这样不太现实吧,要是我已经存储的数据量很大呢?时间间隔太小,几秒钟刷新读取一次,用时间戳没什么用吧。######

    引用来自“huaye2007”的评论

    MD5下字符串  每次比较MD5后的结果

    嗯,这个对读取的是一笔数据时比较容易判断的。
    2020-06-06 21:47:52
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多