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

How to solve the DOS window garbled problem in MySQL

The garbled code problem is as follows: The reaso...

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...

Detailed explanation of simple snow effect example using JS

Table of contents Preface Main implementation cod...

JS achieves five-star praise case

This article shares the specific code of JS to ac...

Vue element implements table adding, deleting and modifying data

This article shares the specific code of vue elem...

HTML Editing Basics (A Must-Read for Newbies)

Open DREAMWEAVER and create a new HTML. . Propert...

Linux uses NetworkManager to randomly generate your MAC address

Nowadays, whether you are on the sofa at home or ...

Sample code for implementing follow ads with JavaScript

Floating ads are a very common form of advertisin...

How to create scheduled tasks using crond tool in Linux

Preface Crond is a scheduled execution tool under...

Vue implements multi-column layout drag

This article shares the specific code of Vue to i...

How to write memory-efficient applications with Node.js

Table of contents Preface Problem: Large file cop...

MySQL backup and recovery design ideas

background First, let me explain the background. ...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...