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.