JavaScript front and back end JSON usage tutorial

  • 2021-09-24 21:21:55
  • OfStack

Summarize the usage of JSON in the front and back of JavaScript, including the mutual conversion of string and JSON object, the traversal of JSON array, the acquisition of key value of JSON object, the formatted output of JSON content to file, and the conversion of JSON content file into JSON object.

1. JavaScript backend JSON operation method

1. JavaScript JSON string to JSON object


var testStr = '{"name":"will","age":18,"hobby":"football"}'
var jsonObj = JSON.parse(testStr)

2. JS JSON object to string


var testObj = {
 "name": 'will',
 "age": '18',
 "hobby": 'football'
}
var jsonStr = JSON.stringify(testObj)

3. Traversal of JavaScript JSON Array

One is for loop traversal:


for (var l = 0; l < jsonArray.length; l++) {
 var jsonItem = jsonArray[l]
}

One is key value traversal:


var testJSArray = [
 {"number": 'one'},
 {"number": "two"},
 {"number": "three"},
 {"number": "four"},
 {"number": "five"},
 {"number": "six"},
]
for(index in testJSArray){
 console.log("index:" + index + "; name:" + testJSArray[index].number)
}

The output is as follows:

index:0; number:one
index:1; number:two
index:2; number:three
index:3; number:four
index:4; number:five
index:5; number:six

4. JS JSON numbers are combined and

The combination and connection uses concat method, and the front end and back end use concat.


var testJSArray01 = [
 {"name": 'one'},
 {"name": "two"},
 {"name": "three"},
]
var testJSArray02 = [
 {"name": "four"},
 {"name": "five"},
 {"name": "six"},
]
var testJSONMerge = testJSArray01.concat(testJSArray02)

5. JavaScript gets the key value of the JSON object


var testObj = {
 "name": 'will',
 "age": '18',
 "hobby": 'football'
}
for (var key in testObj){
 console.log("key:" + key + ", value:" + testObj[key])
}

The output is as follows:

key:name, value:will
key:age, value:18
key:hobby, value:football

6. JS Formatting Output JSON Content to File


var writeStream = fs.createWriteStream(filePath);

return new Promise(function(resolve, reject) {
 writeStream.write(" Write the string content you need ");
 //  Wrap, if it is a character with a new line in the string, write to the .txt You can't break the line when you are in a file, you need to output a newline character. 
 writeStream.write("\n");
 //  Formatted output JSON String content , JSONObj Is to be output JSON Data object 
 writeStream.write(JSON.stringify(JSONObj, null, "\t") + "\n");

 writeStream.end();
 writeStream.on('finish', () => {
 resolve(filePath);
 });
});

7. JavaScript reads the contents of JSON file

Whether the content is saved as. json or. txt, as long as it is a legal JSON string content.


var filePath = 'xxx/xxx/test.json'
var fileContent = fs.readFileSync(filePath).toString();
var fileJson = JSON.parse(fileContent);

2. JavaScript front-end JSON operation method

1. String to JSON, angular. fromJson () is equivalent to JSON. parse ()


var processInfo = angular.fromJson('{"process":[]}');

2. JSON to string, angular. toJson () is equivalent to JSON. stringify ()


var out = angular.toJson(jsonObj, true);

3. JSON Array Traversal


var testObj = {
 "name": 'will',
 "age": '18',
 "hobby": 'football'
}
var jsonStr = JSON.stringify(testObj)
0

4. Judge whether the JSON object contains a certain Key value

For example, determine whether the object jsonObj contains the value "samples" key


var testObj = {
 "name": 'will',
 "age": '18',
 "hobby": 'football'
}
var jsonStr = JSON.stringify(testObj)
1

Summarize


Related articles: