0

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?

3
  • 4
    You're overthinking it man. "JSON" JavaScript Object Notation. You can literally assign the object to a variable. Commented Nov 8, 2013 at 21:17
  • If you have an object, myObj and you want it as JSON (a string) then you use JSON.stringify(myObj). You haven't shown your source object in the question. You've got a qm.data object that doesn't seem to have any relationship with your desired output JSON string. Note that there's no such thing as a JSON object. Commented Nov 8, 2013 at 21:20
  • @nnnnnn json is my source object? or you mean data which I juse in my PHP json encode? Commented Nov 8, 2013 at 21:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

jQuery doesn't have a built-in $.JSON does it? Is that from a plugin?
it has worked with the myObject.stinify() option :) thanks a lot!
Awesome! Glad I could help :) You could help me out, as well, by accepting my answer :D
@nnnnnn: You are corret. jQuery only contains $.parseJSON() natively, to parse a JSON string to an object. Nice catch, I have edited the answer to reflect that.
@mastahb: Are you trying to access it as qm.data[i].main[j].negativesData ? That would be the appropriate syntax, using i as the outer index and j as the inner index (your objects are wrapped in arrays).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.