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"}]
NaN
  • 1,286
  • 2
  • 16
  • 29

3 Answers3

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 );
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • 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 – NaN Nov 15 '13 at 09:48
  • Sirko, is there a way to remove the quotes from the index? I'm getting an error from them – NaN Nov 15 '13 at 10:02
  • @NaN Do you need numerical indexes or a real array? For the former try `obj[ +iVal ] = iKey;`. – Sirko Nov 15 '13 at 10:08
  • 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? – NaN Nov 15 '13 at 10:34
  • 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. – NaN Nov 15 '13 at 10:36
  • @NaN As long as you stay on the client, all strings should be unicode. Maybe the "fix" would just be to declare your document to use utf8? – Sirko Nov 15 '13 at 10:44
  • Thanks Sirko. I'm on HTML5 and I've already got `` at the top of the page. It's really strange that I can use the output by copying and pasting it but yet if I use the variable, it crashes. I'm getting `NetworkError: 403 Forbidden` – NaN Nov 15 '13 at 10:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41249/discussion-between-sirko-and-nan) – Sirko Nov 15 '13 at 10:48
0

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

Tech Mahesh
  • 392
  • 1
  • 3
  • 14
  • Thanks. stringify doesn't give me what I need though. – NaN Nov 15 '13 at 09:49
  • 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+='}'; – Tech Mahesh Nov 15 '13 at 09:54
  • Thanks Tech. I'll also try this. – NaN Nov 15 '13 at 09:59
  • 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. – NaN Nov 15 '13 at 10:38
  • Then you could try this, execs += idx + ':"' + obj.name+'",'; – Tech Mahesh Nov 15 '13 at 11:05
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");
                }
            });
Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84