Response.json()返回的JSON似乎是无效的?

huangapple go评论93阅读模式
英文:

JSON returned from Response.json() appears to be invalid?

问题

Checking the JSON output of my application on https://jsonlint.com/ indicates that the JSON is invalid... But I cannot fathom how it's invalid.

The JSON in question:

{
	"records": [{
		"id": 70,
		"whse": "00",
		"partNo": "100E",
		"description": "1\" EMT CONDUIT (BUNDLE QTY. 1000FT)",
	}],
	"start": 0,
	"limit": 10,
	"count": 1
}

I've tried changing the type of quotes used to no avail. The error returned doesn't really help me at all either:

Error: Parse error on line 1:
{   "records": [{       "id": 70
--^
Expecting 'STRING', '}', got 'undefined'

As far as I can tell, it's valid JSON. This JSON is being returned from a Response.json() function call after a successful fetch().

英文:

Checking the JSON output of my application on https://jsonlint.com/ indicates that the JSON is invalid... But I cannot fathom how it's invalid.

The JSON in question:

{
	records: [{
		id: 70,
		whse: '00',
		partNo: '100E',
		description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
	}],
	start: 0,
	limit: 10,
	count: 1
}

I've tried changing the type of quotes used to no avail. The error returned doesn't really help me at all either:

Error: Parse error on line 1:
{	records: [{		id: 70
--^
Expecting 'STRING', '}', got 'undefined'

As far as I can tell, it's valid JSON. This JSON is being returned from a Response.json() function call after a successful fetch().

答案1

得分: 2

你是正确的,将引号视为潜在的问题源是正确的。

这是一个有效的JavaScript对象,是的。这意味着如果你这样做:

const variable = {
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
};

但这不是一个有效的JSON字符串。如果你想要一个有效的JSON字符串,你需要像这样将其转换为字符串:

JSON.stringify({
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
});

结果将是字符串:

{
  "records": [
    {
      "id": 70,
      "whse": "00",
      "partNo": "100E",
      "description": "1\" EMT CONDUIT (BUNDLE QTY. 1000FT)"
    }
  ],
  "start": 0,
  "limit": 10,
  "count": 1
}

(请注意在键名周围的双引号)

英文:

You were right to think of the quotes as a potential source of problem.

That's a valid JavaScript Object yes. Meaning a JS interpreter will successfully understand it if you were to do:

const variable = {
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
};

but that's not a valid JSON string. if you want valid JSON string, you'd need to stringify it like this:

JSON.stringify({
    records: [{
        id: 70,
        whse: '00',
        partNo: '100E',
        description: '1" EMT CONDUIT (BUNDLE QTY. 1000FT)',
    }],
    start: 0,
    limit: 10,
    count: 1
});

and the result would be the string:

{
  "records": [
    {
      "id": 70,
      "whse": "00",
      "partNo": "100E",
      "description": "1\" EMT CONDUIT (BUNDLE QTY. 1000FT)"
    }
  ],
  "start": 0,
  "limit": 10,
  "count": 1
}

(notice the double quotes around key names)

答案2

得分: 2

这是因为原始的 JSON 需要字符串或整数作为属性,然而,对于 JavaScript 中的 JSON,这是有效的语法。有几件事情使 jsonlint 不喜欢,为了修复这个问题,你必须

  • 为每个属性添加双引号
  • 将每个字符串值更改为双引号
  • 移除对象的最后一个逗号

然后你会得到像这样的结果:

{
    "records": [{
        "id": 70,
        "whse": "00",
        "partNo": "100E",
        "description": "1\" EMT CONDUIT (BUNDLE QTY. 1000FT)"
    }],
    "start": 0,
    "limit": 10,
    "count": 1
}

此外,请考虑,由于你在字符串内部有双引号,你必须使用反斜杠杠来转义该字符。

英文:

That is because raw JSON expects string or ints as property, however, for a javascript JSON its a valid syntax. There are several things that jsonlint didnt liked, in order to fix this you must

  • Add double quotes for each property
  • Change to double quotes for each string value
  • Remove the last comma of the object

And you will get something like this:
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
	&quot;records&quot;: [{
		&quot;id&quot;: 70,
		&quot;whse&quot;: &quot;00&quot;,
		&quot;partNo&quot;: &quot;100E&quot;,
		&quot;description&quot;: &quot;1\&quot; EMT CONDUIT (BUNDLE QTY. 1000FT)&quot;
	}],
	&quot;start&quot;: 0,
	&quot;limit&quot;: 10,
	&quot;count&quot;: 1
}

<!-- end snippet -->

Also, take into consideration that since you have double quotes inside a string you have to use the backslash to escape the character

huangapple
  • 本文由 发表于 2023年1月10日 01:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75060693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定