Summary of how to judge json in javascript _ javascript skills

Source: Internet
Author: User
Tags string to json
JSON (JavaScriptObjectNotation) is a lightweight data exchange format. It uses a language-independent text format and is an ideal data exchange format. At the same time, JSON is the native format of JavaScript, which means that JSON data processing in JavaScript does not require any special APIs or toolkit. Next, let's learn how to judge json in JavaScript, simply put, JSON can convert a set of data in a JavaScript object to a string (pseudo object), and then it can be easily passed between functions, alternatively, the string is transmitted from the Web Client to the server in an asynchronous application. This string looks a bit odd (several examples will be seen later), but JavaScript can easily explain it, and JSON can represent a more complex structure than name/value pairs. For example, it can represent arrays and complex objects, not just a simple list of keys and values.

Determine whether json is null

The Code is as follows:


Var jsonStr = {};

1. Determine whether json is null

The Code is as follows:


JQuery. isEmptyObject ();

2. Check whether the object is empty:

The Code is as follows:


If (typeOf (x) = "undefined ")
If (typeOf (x )! = "Object ")
If (! X)

The third method is the simplest method, but the third method cannot be used to determine the mutex of if (x). It can only be added before the object!

3. json keys cannot be repeated;

The Code is as follows:


JsonStr [key] = "xxx"

If it exists in replacement, it is added if it does not exist.

4. Traverse json

For (var key in jsonStr) {alert (key + "" + jsonStr [key])} isJson = function (obj) {var isjson = typeof (obj) = "object" & Object. prototype. toString. call (obj ). toLowerCase () = "[object]" &! Obj. length; return isjson;} if (! IsJson (data) data = eval ('+ data +'); // converts a string to json format

JSON structure: objects and arrays.

1. Object

An object starts with "{" and ends. Each "key" is followed by a ":", and the "key/value" pairs are separated.

The Code is as follows:


PackJson = {"name": "nikita", "password": "1111 "}

2. Array

The Code is as follows:


PackJson = [{"name": "nikita", "password": "1111" },{ "name": "tony", "password": "2222"}];

An array is an ordered set of values. An array ends with "[" and. Values are separated by commas.

The above section describes the json Judgment Method in js. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.