How to judge json in JavaScript summarize _javascript techniques

Source: Internet
Author: User
Tags convert string to json object object string to json

Simply put, JSON converts a set of data represented in a JavaScript object to a string (pseudo object), which can then be easily passed between functions, or pass a string from a WEB client to a server-side program in an asynchronous application. This string looks a little odd (see some examples later), but JavaScript is easy to explain, and JSON can represent a more complex structure than name/value pairs. For example, you can represent arrays and complex objects, not just simple lists of keys and values.

To determine if JSON is empty

Copy Code code as follows:

var jsonstr ={};

1, to determine whether the JSON is empty

Copy Code code as follows:

Jquery.isemptyobject ();

2, determine whether the object is empty:

Copy Code code as follows:

if (typeOf (x) = = "undefined")
if (typeOf (x)!= "Object")
if (!x)

The third is the simplest method, but the third one cannot be judged by a mutually exclusive method of if (x), only in front of the object.

3, JSON key can not be repeated;

Copy Code code as follows:

jsonstr[key]= "XXX"

exists in the substitution, does not exist is added.

4. Traverse JSON

for (var key in jsonstr) {

  alert (key+ "+jsonstr[key])

Isjson = function (obj) {
  var Isjson = typeof (obj) = = "Object" && (obj). toLowerCase () = = "[Object Object]" &&!obj.length;< C5/>return Isjson;
if (!isjson (data)) data = eval (' (' +data+ ') ');//convert string to JSON format

Structure in JSON: objects and arrays.

1. The object

An object ends with "{" Beginning, "}". Each "key" is followed by a ":", "Key/value" to "use", "separated."

Copy Code code as follows:

Packjson = {"Name": "Nikita", "Password": "1111"}

2. Array

Copy Code code as follows:

Packjson = [{"Name": "Nikita", "Password": "1111"}, {"Name": "Tony", "Password": "2222"}];

An array is an ordered collection of values. An array ends with "[" Beginning, "]". Values are separated by ",".

The above is the introduction of JS JSON in the judgment method, I hope you like.

Related Article

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: 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.