jsonview jquery usage

WBOY
Release: 2023-05-28 18:45:38
Original
871 people have browsed it

JSONView is a very convenient browser plug-in for viewing JSON format data in the browser. In this article, we will introduce how the JSONView plugin works with jQuery, allowing us to easily view JSON data in a readable way.

JSONView browser plug-in installation

First, we need to install the JSONView plug-in in our browser. JSONView is available in many different versions, including ones for browsers such as Chrome, Firefox, and Safari. We will cover the Chrome and Firefox versions of the JSONView plugin here. You can access them via the following link:

  • Chrome JSONView: https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc
  • Firefox JSONView: https:// addons.mozilla.org/zh-CN/firefox/addon/jsonview/

After installing JSONView, you can format JSON-formatted data by right-clicking on it and selecting "JSONView: Format JSON" ize and view data.

Using jQuery and JSONView plug-ins

In order to use jQuery and JSONView plug-ins, the scripts of jQuery and JSONView plug-ins must first be imported into the page. We can achieve this by adding the following tag to the page:

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://raw.githubusercontent.com/bhollis/jsonview/master/src/jquery.jsonview.js"></script>
Copy after login

Next, we need to define a JavaScript object that contains the JSON data. In this article, we will use the following sample JSON data:

var person = {
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": 12345
  },
  "phone": [
    {
      "type": "home",
      "number": "555-1234"
    },
    {
      "type": "work",
      "number": "555-5678"
    }
  ]
};
Copy after login

Now we can use jQuery and the JSONView plugin to view this JSON data in a readable way. We can use the following code in the page to achieve this:

$(document).ready(function() {
  // Define the JSON data
  var person = {
    "name": "John Doe",
    "age": 30,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": 12345
    },
    "phone": [
      {
        "type": "home",
        "number": "555-1234"
      },
      {
        "type": "work",
        "number": "555-5678"
      }
    ]
  };

  // Convert the JSON data to a string
  var jsonData = JSON.stringify(person);

  // Use the JSONView plugin to format and display the JSON data
  $('#json').JSONView(jsonData);
});
Copy after login

This code first converts the JSON data into a string, and uses the JSONView plug-in to format and display it in the HTML element with the ID "json" .

When using the JSONView plugin, you can also pass options to customize the representation. In this article, we will use the following options:

{
  collapsed: false,
  recursive_collapser: true,
  output_padding: true
}
Copy after login

These options will make the JSON data always expanded, recursively collapsed, and include some extra whitespace in the output for increased readability. Therefore, we can use the following code to apply custom options:

$(document).ready(function() {
  // Define the JSON data
  var person = {
    "name": "John Doe",
    "age": 30,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": 12345
    },
    "phone": [
      {
        "type": "home",
        "number": "555-1234"
      },
      {
        "type": "work",
        "number": "555-5678"
      }
    ]
  };

  // Convert the JSON data to a string
  var jsonData = JSON.stringify(person);

  // Define the options for JSONView
  var jsonOptions = {
    collapsed: false,
    recursive_collapser: true,
    output_padding: true
  };

  // Use the JSONView plugin to format and display the JSON data with the custom options
  $('#json').JSONView(jsonData, jsonOptions);
});
Copy after login

In summary, we have seen how to view JSON data in a readable way using jQuery and the JSONView plugin. JSONView is a very useful browser plug-in that makes it easy to format and browse JSON-formatted data. By combining jQuery, we can make viewing JSON data more convenient and customizable.

The above is the detailed content of jsonview jquery usage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template