Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell which JSON object is being used (Crockford's or another)?

I'm using Crockford's json2.js. When I want to stringify, I do JSON.stringify()...works great.

However, those who've taken a look at the code know that it defers to existing JSON objects and properties. I suspect a certain problem I'm having may be due to this deference.

Is there a property of the JSON object I can check to see if the browser is using Crockford's object, or some other? It would be nice to be able to do something like alert(JSON.version());

like image 424
Jonathan M Avatar asked Jun 22 '12 05:06

Jonathan M


People also ask

How do you check if an object is a JSON object?

To check if JavaScript object is JSON, we can use the JSON. parse method. const isJson = (data) => { try { const testIfJson = JSON. parse(data); if (typeof testIfJson === "object") { return true; } else { return false; } } catch { return false; } };

What is the type of JSON object in JavaScript?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

How do you check key is exist or not in JSON?

jquery json provide several method for getting key value or check if key exists etc. In this example we will use hasOwnProperty method of json object that will help to check if key exists or not in jquery. hasOwnProperty return true if key is exists and return false if key is not exists on given javascript json.

How do I access a JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How to check if a JSON resource is an object?

JSON encoded resource is not an object. It is a string. Only after you decode it or in Javascript JSON.parse () it does the JSON resource become an object. Therefore if you test a resource coming from a server to see if it is JSON, it is best to check first for String, then if is a not a <empty string> and then after parsing if it is an object.

What is the syntax for writing a JSON object?

Object Syntax. Example. { "name":"John", "age":30, "car":null } JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.

How do I check the type of an object in JSON?

If you are trying to check the type of an object after you parse a JSON string, I suggest checking the constructor attribute: obj.constructor == Array || obj.constructor == String || obj.constructor == Object. This will be a much faster check than typeof or instanceof.

How to test the color of an object in JSON?

Our JSON file will look something like this: To test e.g. the color of our second item, we will to combine two mentioned approaches - referencing array item & referencing object key: With cars [1], we are selecting the second item from our array of objects, and within that object, we are using .color to select the proper value from that object.


2 Answers

You can decide one to use like this:

<script>window.JSON || document.write('<script src="js/json2.js"><\/script>')</script>

This checks for window.JSON (supported by browser) first if it exists, use that else imports json2.js of Crockford.

update

var whichJSON = null;

if (! window.JSON) {
  document.write('<script src="js/json2.js"><\/script>');
  whichJSON = 'Crockford Version';
}
else {
  whichJSON = 'Browser Native Version';
}

alert(whichJSON);
like image 116
Sarfraz Avatar answered Oct 09 '22 09:10

Sarfraz


Before you load Crockford's script, you can check for the global JSON object exactly like he does:

<script>
    var JSON,
        nativeJSON = true;

    if (!JSON) {
        var nativeJSON = false;
        document.write('<script src="js/json2.js"><\/script>');
    }

    if (!nativeJSON) {
        // All JSON objects are using Crockford's implementation
    } else {
        // All JSON objects from here on out are NOT using Crockford's implementation
    }
</script>
like image 29
Big McLargeHuge Avatar answered Oct 09 '22 08:10

Big McLargeHuge