Home  >  Article  >  Web Front-end  >  How to operate json with jquery

How to operate json with jquery

DDD
DDDOriginal
2023-10-13 09:49:401016browse

Jquery method of operating json: 1. "$.parseJSON(jsonString)" 2. "$.getJSON(url, data, success)"; 3. "$.each(obj, callback)"; 4. "$.ajax()".

How to operate json with jquery

In jQuery, you can use the following methods to manipulate JSON data:

1. `$.parseJSON(jsonString)`: Convert a Convert JSON string to JavaScript object. For example:

var jsonString = '{"name":"John", "age":30, "city":"New York"}';
var jsonObject = $.parseJSON(jsonString);
console.log(jsonObject.name); // 输出:John

2. `$.getJSON(url, data, success)`: Get JSON data from the server and convert it into a JavaScript object. For example:

$.getJSON("data.json", function(data) {
  console.log(data.name); // 输出:John
});

3. `$.each(obj, callback)`: Traverse a JavaScript object or array and execute the callback function on each element. For example:

var jsonObject = {"name":"John", "age":30, "city":"New York"};
$.each(jsonObject, function(key, value) {
  console.log(key + ": " + value);
});

4. `$.ajax()`: Use AJAX to get JSON data from the server and perform more complex operations. For example:

$.ajax({
  url: "data.json",
  dataType: "json",
  success: function(data) {
    console.log(data.name); // 输出:John
  }
});

These are some commonly used jQuery methods for manipulating JSON data. Depending on your specific needs, you can choose a method that suits you to manipulate JSON data.

The above is the detailed content of How to operate json with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn