5

I am trying to take a JSON object and build a JSON string but I'm not sure how to do it.

This is what I have so far that is giving me the correct output.

var execs = '';
$.each(window.ob.executives, function(idx, obj) {
    execs = idx + ':' + obj.name;
});

What I need is a string like this:

{ 1: 'test1', 2: 'test2', 3: 'test3', 4: 'test4' }

Can someone show me how to build this string?

Also, you'll probably notice that I am using a window variable which I understand isn't good. If someone can tell me how to get the contents of this variable, which is in another function, that would be greatly appreciated.

EDIT: stringify won't give me what I need guys. Here is what I get with that:

[{"test1":"1","test2":"2"},{"test3":"3","test4":"4"}]
0

3 Answers 3

4

No need for jQuery here:

var execs = JSON.stringify( window.ob.executives );

Edit

After OP specifying the structure of the variable, I suggest the following (Traversing through two levels of nested objects, extracting the data to add it to an intermediate object, which can then be serialized):

var obj = {};
$.each(window.ob.executives, function( key, val ) {
  $.each( val, function( iKey, iVal ) {
    obj[ iVal ] = iKey;
  });
});
var execs = JSON.stringify( obj );
Sign up to request clarification or add additional context in comments.

8 Comments

Sirko, thanks. This isn't giving me what I need. Here is what I get: [{"test1":"1","test2":"2"},{"test3":"3","test4":"4"}]. What I need are all the index and values inside one pair of {} brackets
Sirko, is there a way to remove the quotes from the index? I'm getting an error from them
@NaN Do you need numerical indexes or a real array? For the former try obj[ +iVal ] = iKey;.
Sirko, I actually got that fixed. I just went in and made a couple of changes on the backend code. There is something that is causing an error though. It seems that the returned JSON string is being interpreted as unicode. Is this right?
If I take the output from the alert box and paste it in the function using it, it works perfectly. However, if I use the execs var, I get an error and the console shows me unicode.
|
0

You can use JSON.stringify(JSON Object) function, Which Converts JSON object to JSON String.

5 Comments

Thanks. stringify doesn't give me what I need though.
Then you can try this, var execs = '{'; $.each(window.ob.executives, function(idx, obj) { execs += idx + ':' + obj.name+','; }); execs=execs.substring(0,(execs.length-1)); execs+='}';
Thanks Tech. I'll also try this.
Hey Tech, this does work as well but the only issue I am having is that there are no quotes around the values, which makes things break. Thank you for this though.
Then you could try this, execs += idx + ':"' + obj.name+'",';
0

Use this code JSON.stringify(data);

For eg:

   $.ajax({
                    type: "POST",
                    url: "/Item/Create",
                    data: JSON.stringify({ "item": item, "status": status }),
                    dataType: 'json',
                    contentType: 'application/json;',
                success: function (data) {
                    },
                error: function (data) {
                    TestAlert("Error");
                }
            });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.