简体   繁体   中英

putting json into javascript variable

I want to put a JSON object into a javascript variable as a sting in order to create a graph.

qm.createGraphData = function() {
$.post("ajax_getGraphDataWebsite    ", function(json) {
           qm.negativesData = json;
    },"json");  

    qm.data = [{
        "xScale":"ordinal",
        "comp":[],
        "main":[{
            "className":".main.l1",
            qm.negativesData},{
            "className":".main.l2",
            qm.negativesData}],
        "type":"line-dotted",
        "yScale":"linear"}];
}

the string value should be added to the "data" section. Now the object get's added but I need to add the string value to the variable like the sample below:

{"data":[{"x":"3283581","y":"2013-10-16"},{"x":"1512116","y":"2013-10-17"},{"x":"3967","y":"2013-10-18"},{"x":"1094","y":"2013-10-19"},{"x":"853","y":"2013-10-20"},{"x":"1205","y":"2013-10-21"},{"x":"2618700","y":"2013-10-22"},{"x":"3928291","y":"2013-10-23"},{"x":"3670318","y":"2013-10-24"},{"x":"3347369","y":"2013-10-25"},{"x":"2525573","y":"2013-10-26"},{"x":"3224612","y":"2013-10-27"},{"x":"3992964","y":"2013-10-28"},{"x":"3949904","y":"2013-10-29"},{"x":"3568618","y":"2013-10-30"},{"x":"3104696","y":"2013-10-31"},{"x":"3246932","y":"2013-11-01"},{"x":"2817758","y":"2013-11-02"},{"x":"3198856","y":"2013-11-03"},{"x":"3952957","y":"2013-11-04"},{"x":"3934173","y":"2013-11-05"},{"x":"3878718","y":"2013-11-06"},{"x":"3642822","y":"2013-11-07"},{"x":"3186096","y":"2013-11-08"}]}

This would generate the right graph for me. Does anyone know how to convert the json object into a string like above and to send it to the qm.negativesData variable?

// UPDATE

Now I've got the string with the qm.negativesData = JSON.stringify(json); solution

But my qm.negativesdata won't get added to the qm.data variable... i'm getting a console error SyntaxError: invalid property id

I suppose i'm not adding them the right way?

To convert a JSON object into a JSON string, you can try myObject.stringify() , JSON.stringify(myObject) , or if you are using a library using the built in function of that library.

So, you could do something like: qm.negativesData = myObject.stringify()

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM