SoFunction
Updated on 2025-04-03

Summary of the method of judging json in javascript

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

Determine whether json is empty

Copy the codeThe code is as follows:

var jsonStr ={};

1. Determine whether json is empty

Copy the codeThe code is as follows:

();

2. Determine whether the object is empty:

Copy the codeThe code is as follows:

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

The third one is the easiest method, but the third one cannot be judged by the mutually exclusive method such as if (x), and can only be added before the object!

3. The json key cannot be repeated;

Copy the codeThe code is as follows:

jsonStr[key]="xxx"

If it exists, it will be replaced. If it does not exist, it will be 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]" && !;
  return isjson;
}
if (!isJson(data)) data = eval('('+data+')');//Convert string tojsonFormat

Structures in JSON: Objects and arrays.

1. Object

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

Copy the codeThe code is as follows:

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

2.Array

Copy the codeThe code is as follows:

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

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

The above is the method of judging json in js introduced in this article. I hope you like it.