Nodejs converts JSON string into JSON object error solution

Nodejs converts JSON string into JSON object error solution

How to convert a JSON string into a JSON object?

JSON.parse(str) JSON is a built-in object in JavaScript, which provides methods for converting JSON objects to strings and vice versa;

Here comes the problem. I understand the principle but it still reports an error!

This is one of my JSON files, which contains some defined JSON test data;

This is the core code;

This is an error message;

Why is the error reported?

Because there is a space at the beginning of my json file. That's it.

OK, modify the code and test

Use trim to remove extra spaces at the beginning

ok, passed!

Nodejs parses JSON data

In nodejs, you don’t need to import the json2.js file like in js, you can directly operate the JSON object

Convert string to JSON

var str = "{ ' a ' : ' 1 ' , ' b ' : ' 2 ' }";
var json = JSON.parse(str);

JSON to string

var json = {
   a : ' 1 ',
   b : ' 2'
};
var str = JSON.stringify(json);

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Java reads the project json file and converts it into a JSON object
  • JavaScript object-oriented class inheritance case explanation
  • jsp Response object page redirection, dynamic display of time
  • Examples and comparison of 3 methods for deduplication of JS object arrays
  • An article to help you understand JavaScript-Object

<<:  Ubuntu 18.04 obtains root permissions and logs in as root user

>>:  Import csv file into mysql using navicat

Recommend

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...

Tutorial on how to deploy LNMP and enable HTTPS service

What is LNMP: Linux+Nginx+Mysql+(php-fpm,php-mysq...

A brief discussion on CSS3 animation jamming solutions

Why is it stuck? There is a premise that must be ...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

How to manually install MySQL 5.7 on CentOS 7.4

MySQL database is widely used, especially for JAV...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

Apache Calcite code for dialect conversion

definition Calcite can unify Sql by parsing Sql i...

Details on how to use class styles in Vue

Table of contents 1. Boolean 2. Expression 3. Mul...

CSS border half or partially visible implementation code

1. Use pseudo-classes to display half of the Bord...

How to solve the error of connecting to the database when ServerManager starts

Servermanager startup connection database error R...

How to authorize all the contents of a folder to a certain user in Linux?

【Problem Analysis】 We can use the chown command. ...

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

mysql query data for today, this week, this month, and last month

today select * from table name where to_days(time...

How to use vue3+TypeScript+vue-router

Table of contents Easy to use Create a project vu...