开发者

How do I convert this javascript into JSON

How do I structure my data such that say, there is one question that associates with 5 choices, and each choice has a vote associated with it? if you think of it like a tree, the question is the root which has 5 leaves,the choices, and each choice has only one leaf, votes.

 var myJSONObject = {"bindings": [
    {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
    {"ircEvent": "PRIVMSG", "method":

the reason why I want to do this is that i want to have a tree-wise structure after 开发者_如何学JAVAI stringify my javascript. the piece of code above is from JSON.org, I guess I want the structure like that "bindings" has 3 ircEvents(leaves), I just dont know how to build this from backward(from javascript) Thank you


See update below

If you have a JavaScript object already, use JSON.stringify on it to get the equivalent JSON string. There's an implementation of JSON.stringify in json2.js by Douglas Crockford (originator of JSON) on his github page. An increasing number of browsers have it built in now that it's been standardized, but many (many) do not, so for now we still do have to use a utility script (Crockford's or another one).

Fundamentally, it's quite easy to generate JSON from an object graph (that was one of the points of JSON, to be something easily produced and consumed).

JSON is a subset of JavaScript Object Literal notation. So this JavaScript (not JSON) object:

{"bindings": [
    {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}
    ]
}

looks exactly like that in JSON. If you were going to put it inside a file you would read into memory or from a server or whatever, that's what you'd put. If you were going to embed a string containing JSON inside JavaScript code (perhaps an odd thing to do, but), you'd just wrap that in quotes:

var aJSONString = '{"bindings": [' +
    '{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},' +
    '{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}' +
    ']' +
'}';

If you JavaScript literal had been written without quotes around the key names, or using single quotes, you'd have to change that because JSON requires specifically that key names and all strings be in double quotes. So this JavaScript object literal:

{foo: 'bar'}

becomes

{"foo": "bar"}

in JSON.


Update: Re your comment below: Sorry, I misunderstood the question.

Building tree structures in JavaScript is dead easy. JavaScript objects are fundamentally fairly freeform collections of key/value pairs (e.g., "maps" or "dictionaries"). So:

// Start with a blank object
var obj = {};

// Add a leaf to it
obj.foo = {};

// Add a leaf under the leaf, with the value 42
obj.foo.subfoo = 42;

// Add a new leave to the root; this one's an array with 1, 2, and 3 in it
obj.bar = [1, 2, 3];

alert(JSON.stringify(obj));
// '{"foo: {"subfoo": 42}, "bar": [1, 2, 3]}'

You can write that as a literal, of course, but I think you know that:

var obj = {
    foo: {
        subfoo: 42
    },
    bar: [1, 2, 3]
};

What you may not know is that the things on the right-hand side in a literal can be variables; their value will be used, just like with an assignment. So this gives me exactly the same obj as the above:

var fortytwo = 42;
var obj = {
    foo: {
        subfoo: fortytwo
    },
    bar: [1, 2, 3]
};


Simplest way to convert JSON text to JS object use "eval()".

var obj = eval('(' + myJSONtext + ')');

However eval() has security issues as we can compile and execute any javascript code within it. Therefore "parse" is recomended to convert JSON text to JS object.

var obj = JSON.parse(myJSONtext);

To convert JS object to JSON Text use stringify.

var myJSONText = JSON.stringify(obj);

See here for more details. http://json.org/js.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜