Prototype has hash.inspect() method. What is the e

2020-07-10 10:41发布

I am using jQuery. I am dealing with JSON object and time and again I need to look at the data. I do alert(data) and I get nothing useful.

In the Prototype world they have inspect method which is highly useful. inspect method in Prototype

I am looking for equivalent method in jQuery. I looked at the API and couldn't find anything. I am sure someone would have developed some plugin to solve this problem.

标签: jquery
5条回答
beautiful°
2楼-- · 2020-07-10 11:03

If you are using FireBug, you can just call console.log(myJsonObject), and FireBug will give you a nice display of your JSON object in the console.

查看更多
戒情不戒烟
3楼-- · 2020-07-10 11:06

Also, firefox and other good browsers support toSource() method on objects and functions.

alert(foo.toSource())

查看更多
Anthone
4楼-- · 2020-07-10 11:11

You can do this, too...

$.getJSON("some/url/here", { /* optional params */ }, function(json) {
    alert("get returned: " + json.toString());
});
查看更多
欢心
5楼-- · 2020-07-10 11:16

You can use the jQuery.param function, which will format it into a querystring format.

alert(jQuery.param({ width:1680, height:1050 }));
// shows "width=1680&height=1050"
查看更多
何必那么认真
6楼-- · 2020-07-10 11:23

I have the best result with http://www.JSON.org/json2.js. As the docs say:

    JSON.stringify(value, replacer, space)
        value       any JavaScript value, usually an object or array.

        replacer    an optional parameter that determines how object
                    values are stringified for objects. It can be a
                    function or an array of strings.

        space       an optional parameter that specifies the indentation
                    of nested structures. If it is omitted, the text will
                    be packed without extra whitespace. If it is a number,
                    it will specify the number of spaces to indent at each
                    level. If it is a string (such as '\t' or ' '),
                    it contains the characters used to indent at each level.

Just include the library and call alert(JSON.stringify(data)) to see a legible representation of your object.

查看更多
登录 后发表回答