DWQA QuestionsCategory: ProgramHow to write JSON string?
ShiRouMi asked 1 month ago

Today, there is a field returned in the data sent to me by the background

tags: "{"area":"XXX","industry":"XXX","kind":"","other":"XXX"}"

This JSON string format is obviously wrong,JSON.parseParsing it will also report an error. This value should be returned as

tags: '{"area":"XXX","industry":"XXX","kind":"","other":"XXX"}'

However, I did not find the information specificationJSON stringOfformatHow should it be, and why the data format returned by the background is wrong, please give me the trouble you know, thank you~
MDN–JSONThere is no introduction here

ars_qu replied 1 month ago

Isn’t this the format specification of JSON? When you want to return a string the day after tomorrow, just spell single quotation marks. If you really can’t, just turn it by yourselfObjects are represented as key value pairs, data are separated by commas, curly brackets save objects, and square brackets save arrayshttp://www.w3school.com.cn/js

5 Answers
Li Yong answered 1 month ago

I haven’t seen such a lovely backstage development for a long time

Flowers bloom on the other side answered 1 month ago

Let the backstage give you this{tags: {"area":"XXX","industry":"XXX","kind":"","other":"XXX"}
Or youJSON.parse('{"area":"XXX","industry":"XXX","kind":"","other":"XXX"})
in addition"{"area":"XXX","industry":"XXX","kind":"","other":"XXX"}"What the hell is it? The grammatical format is wrong

Pineapple of Borodin answered 1 month ago

‘{“key”: “value”, “number”: 1, “object”: {“name”: “this is my name”}}} in this format, the number can be added with double quotation marks or not

pumpkin head answered 1 month ago

It is true that there is no specification to specify the format of JSON string. JSON itself is just a string, which is convenient for data transmission.
Your problem is just a simple data format error, which belongs to the problem of not understanding JSON.
And now that the back-end framework is so mature, shouldn’t there be ready-made API conversion? You back-end students have written strings to you manually?

Unique, unique answered 1 month ago

The most basic.

  • The key must be wrapped in double quotation marks.
  • The last object in an array must not be followed by a,
  • Object usage{ }Package, array use[ ]Package.
  • Others are basically the same as the objects in JS.