📜  json.objectify - Javascript (1)

📅  最后修改于: 2023-12-03 15:32:25.347000             🧑  作者: Mango

json.objectify - Javascript

json.objectify is a Javascript library for parsing JSON strings into Javascript objects. It supports JSON strings with circular references and functions as values.

Installation

You can install json.objectify via npm:

npm install json.objectify

Or you can include it in your HTML file:

<script src="https://unpkg.com/json.objectify"></script>
Usage

To use json.objectify, simply call the parse method with a JSON string as the argument:

const jsonString = '{ "name": "John", "age": 30 }';
const obj = json.objectify.parse(jsonString);
console.log(obj.name); // Output: John
console.log(obj.age); // Output: 30

You can also parse JSON strings with circular references:

const jsonString = '{ "foo": { "bar": { "baz": "Hello" } } }';
const obj = json.objectify.parse(jsonString);
obj.foo.bar.baz = obj;
const jsonStringCircular = JSON.stringify(obj);
const objCircular = json.objectify.parse(jsonStringCircular);
console.log(objCircular.foo.bar.baz.foo.bar.baz.foo.bar.baz); // Output: [Circular]

And JSON strings that contain functions as values:

const jsonString = '{ "foo": function() { console.log("Hello"); } }';
const obj = json.objectify.parse(jsonString);
obj.foo(); // Output: Hello
API
parse(jsonString: string): object

Parses a JSON string into a Javascript object. The jsonString argument must be a valid JSON string.

Conclusion

json.objectify is a versatile library for parsing JSON strings into Javascript objects. With support for circular references and functions as values, it makes working with JSON data in Javascript a breeze.