Home > Web Front-end > JS Tutorial > body text

What are the methods for JS to judge json?

php中世界最好的语言
Release: 2018-04-25 09:36:21
Original
2350 people have browsed it

This time I will bring to you what methods JS can use to judge json, and what are the precautions for JS to judge json. The following is a practical case, let’s take a look.

JSON(JavaScript Object Notation) is a lightweight data exchange format that uses a completely 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 processing JSON data in JavaScript does not require any special API or toolkit. Next, follow the editor to learn how to judge json in js

Simply put, JSON can convert a set of data represented in a JavaScript object into a string (pseudo-object). This string can then be easily passed between functions or from a string in an asynchronous application. The web client passes it to the server-side program. This string looks a little weird (you'll see a few examples later), but JavaScript interprets it easily, and JSON can represent more complex structures than name/value pairs. For example, arrays and complex objects can be represented rather than just simple lists of keys and values.

Judge whether json is empty

var jsonStr ={};
Copy after login

1.Judge whether json is empty

jQuery.isEmptyObject();
Copy after login

2.Judge Whether the object is empty:

if   (typeOf(x)   ==   "undefined")
if   (typeOf(x)   !=   "object")
if(!x)
Copy after login

The third method is the simplest method, but the third method cannot be judged by the mutually exclusive method if (x), it can only be judged in front of the object add!

3. The json key cannot be repeated; if

jsonStr[key]="xxx"
Copy after login

exists, it will be replaced, if it does not exist, will be added .

4. Traverse the structures in 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 object]" && !obj.length;
  return isjson;
}
if (!isJson(data)) data = eval('('+data+')');//将字符串转换成json格式
Copy after login

: objects and arrays.

1. Object

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

packJson = {"name":"nikita", "password":"1111"}
Copy after login

2. Array

packJson = [{"name":"nikita", "password":"1111"}, {"name":"tony", "password":"2222"}];
Copy after login

An array is an ordered collection of values. An array starts with "[" and ends with "]". Use "," to separate values.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the differences between ajax and jsonp and the steps to use json

How to use JSON as a function parameter

The above is the detailed content of What are the methods for JS to judge json?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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