1
var DTO = [];

$.each(data.Foobars, function(i, val){
    DTO.push(val);
});

//example of a stringified object in the array:
// var val = {"a":"1","b":"2","c":"3"};

var jsonString = JSON.stringify(DTO); 

With one object in the array the ´jsonString´ would look like:

[{"a":"1","b":"2","c":"3"}]

With more than one:

[[{"a":"1","b":"2","c":"3"}, {"a":"1","b":"2","c":"3"}]]

Resulting in double brackets, which is causing me some problems serverside.

How can i get rid of the double brackets? Thanks

6
  • Causing you more problems server-side? The JavaScriptSerializer should have no problems with that :) Commented May 22, 2012 at 11:58
  • Have you tried console.log(DTO)? Commented May 22, 2012 at 11:59
  • @mattytommo im using JSON.net and it has some problems with it. Commented May 22, 2012 at 12:01
  • @Jack Yes, you see the stringified versions above Commented May 22, 2012 at 12:02
  • @Johan that's what comes from JSON.stringify(); but have you inspected the value of DTO on the console? Commented May 22, 2012 at 12:06

3 Answers 3

4

Are you sure the val object is not an array itself?

In my little test: http://jsfiddle.net/NatJS/, JSON.stringify works just fine.

Update: if val is an array, you need to treat it appropriately, such as: http://jsfiddle.net/NatJS/1/

var a = [{ a: 1, b: 2, c: 3},{ d: 10, e: 11, f: 12}];
//var a = { a: 1, b: 2, c: 3}
var j = [];


if (a.length) { // cheap way to check if a an an array; implement something like mooTools.typeOf if you want something more robust
    while (a.length > 0) {
        j.push(a.pop());               
    }
}
else {
    j.push(a);
}

console.log(JSON.stringify(j));​
Sign up to request clarification or add additional context in comments.

2 Comments

See me update above, you should check for the type of val and treat it appropriately. If you're pushing an array into an array, JSON.stringify behaves exactly like it should :)
I used typeof to check if it was an array. Should have used $.isArray. Solved it with jquerys $.makeArray (only makes one if necessary). Thanks
1

The comment area is too small for this.

Take a look at the following simplification of your problem:

var DTO = [];

DTO.push({"a":"1","b":"2","c":"3"});
DTO.push({"a":"1","b":"2","c":"3"});

console.log(JSON.stringify(DTO));

It will show:

[{"a":"1","b":"2","c":"3"},{"a":"1","b":"2","c":"3"}]

Conclusion: whatever you think val should contain it's not ;-)

1 Comment

My problem seems to be that if data.Foobars.length == 1 then val will contains the values of the object. 1,2,3. But if there are more than one object, val will be the entire object...
0

Check if toJSON is defined on your object. If it is defined, it can modify the behavior of stringify.

Comments

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.