簡體   English   中英

比較兩個 JSON 文件

[英]Compare two JSON files

我正在嘗試比較賽普拉斯中的兩個 JSON 文件。

為了看看這是否有效,我只是復制了data.json並將其副本重命名為data2.json

var comparejson = cy.readFile('data2.json')
cy
  .readFile('data.json')
  .then(json => JSON.stringify(json)).should('eq', JSON.parse(comparejson))

這是我得到的錯誤:

SyntaxError: Unexpected token o in JSON at position 1

啊,可怕的token o 它正在從[object Object]讀取“o”,這是普通 Ol' JavaScript Object 的toString表示。

順便說一句,您可以自己測試這個,方法是輸入 JavaScript REPL 並:

08:54 $ node
Welcome to Node.js v13.0.1.
Type ".help" for more information.
> JSON.parse({}.toString())
Thrown:
SyntaxError: Unexpected token o in JSON at position 1
> ({}).toString()
'[object Object]'

所以在未來,任何時候你看到這個錯誤,你就知道你已經跳過了在某個地方進行字符串化的步驟!

The trick here is that readFile returns an object (not a string, JSON files are parsed by Cypress into JavaScript ) but you're calling JSON.parse on the object.

嘗試這個:

cy
  .readFile('data2.json')
  .then(data2 => cy.readFile('data.json').should('deep.equal', data2))

注意這里使用deep.equal ,因為我們是在比較對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM