简体   繁体   English

为什么 JSON output 中的某些字段缺少引号以及为什么 output 中没有双引号

[英]Why are some fields in JSON output missing quotes and why no double quotes in output

Below is screenshot of vs code terminal output.下面是vs代码终端output的截图。 The JSON output is treating the 3 fields with red arrows differently than the others. JSON output 对带有红色箭头的 3 个字段的处理方式与其他字段不同。 The string has double quotes around easch item but tje JSOn.parse output has single quotes' For test purposes, code is set to handle 1 listing.该字符串在 easch 项目周围有双引号,但 tje JSOn.parse output 有单引号'出于测试目的,代码设置为处理 1 个列表。

在此处输入图像描述

Here is code这是代码

 const request = require('request-promise'); const cheerio = require('cheerio'); const fs = require('fs'); const { JSDOM } = require("jsdom"); fs.readFile('./data/hb2019.html', 'utf-8', (err, data) => { if (err) { console.log(err); return; } const dom = new JSDOM(data); fieldName = []; fieldName = [...dom.window.document.querySelectorAll('#tableComparableSales >tbody.salesReportSortLink')] //file may artificially have multiple pages var listings = [];// array of raw listing links listings = dom.window.document.querySelectorAll('[id^=tdSales]'); //var output = '['; var output = ''; (function () { field = []; //console.log(listings.length) var numListings = 1;//28 var cleanedField; for (var i = 0; i < numListings; i++) { output = output + '{'; for (var j = 1; j < 16; j++) { field[i] = dom.window.document.querySelector(`#tdSales${i + 1}>td:nth-child(${j})`); //some fields have quotes in them ie> Financial inst or "In Lieu of Forclosure" stated" cleanedField = field[i].textContent.trim().replace(/\"/g, ""); output = output + `"${fieldName[j - 1].textContent.trim()}":"${cleanedField}"`; output = output.replace(/\s+/g, ' '); // don't output last comma if (j < 15) { output = `${output},`; }; }; // don't output last comma if (i < numListings - 1) { output = output + '},'; } else { output = output + '}'; } } //output = output + ']'; output = output.replace(/\s+/g, ' '); console.log(output); var obj = JSON.parse(output); console.log(obj); })(); });

Seems issue with this line "${fieldName[j - 1].textContent.trim()}":"${cleanedField}" .这一行似乎有问题"${fieldName[j - 1].textContent.trim()}":"${cleanedField}" Since it is a template literals so " is not required由于它是模板文字,因此不需要"

It is not treating them differently, it is just showing them differently.不是以不同的方式对待它们,而是以不同的方式展示它们。

It is using quotes whenever a key contains characters not valid in Javascript variable names, eg space or # .只要键包含在 Javascript 变量名称中无效的字符,例如空格或# ,它就会使用引号。

The same way that you can write in Javascript obj.var1 or obj['var1'] but you cannot write obj.var2% , you have to write obj['var2%'] .与您可以在 Javascript obj.var1obj['var1']中编写但您不能编写obj.var2%的方式相同,您必须编写obj['var2%']

BTW, this output is a text representation of a javascript object, but it is not JSON.顺便说一句,这个 output 是 javascript object 的文本表示,但它不是Z0ECD11C1D7A3BB87401D8148A2。 In JSON, all object keys and strings are enclosed in double quotes " . The line above is proper JSON.在 JSON 中,所有 object 键和字符串都用双引号括" 。上面的行正确的 JSON。

The problem was that I was copying the output string from the vsCode terminal and pasting iinto the validator (as plain text) Apparently that introduces line breaks.问题是我正在从 vsCode 终端复制 output 字符串并将 i 粘贴到验证器中(作为纯文本)显然引入了换行符。 So I saved the string to a file and then uploade4 the file to the json validator and it validated perfectly.所以我将字符串保存到一个文件中,然后将文件上传到 json 验证器,它验证得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM