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

Simple comparison of meta tags in html

The meta tag is used to define file information an...

Vue3.0 uses the vue-grid-layout plug-in to implement drag layout

Table of contents 1. Plugins 2. Interlude 3. Impl...

Example code of Vue3 encapsulated magnifying glass component

Table of contents Component Infrastructure Purpos...

Implementing carousel effects with JavaScript

This article shares the specific code for JavaScr...

In-depth explanation of Vue multi-select list component

A Multi-Select is a UI element that lists all opt...

Web designers should optimize web pages from three aspects

<br />With the increase of bandwidth, there ...

Learn the operating mechanism of jsBridge in one article

Table of contents js calling method Android 1.js ...

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

VMware + Ubuntu18.04 Graphic Tutorial on Building Hadoop Cluster Environment

Table of contents Preface VMware clone virtual ma...

Introduction to html form control disabled attributes readonly VS disabled

There are two ways to disable form submission in ...

How to upgrade MySQL 5.6 to 5.7 under Windows

Written in front There are two ways to upgrade My...

Nginx/Httpd load balancing tomcat configuration tutorial

In the previous blog, we talked about using Nginx...

Uninstalling MySQL database under Linux

How to uninstall MySQL database under Linux? The ...