0

Ok, this question may be dumb, but I'm really stuck with this.

I want to a json structure like the following:

order: {
    currency: 'eur',
    order_items: [
        {
            id: 3,
            quantity: 1
        },
        {
            id: 67,
            quantity: 1
        }
    ]
}

What I'm doing right now is this:

function makeOrder(){
    var myArray = new Array();
    for(var i=0;i<bookedItemsArray.length;i++){
        var newObject = new OrderedItem(bookedItemsArray[i].id, bookedItemsArray[i].amount);
        myArray.push(newObject);
    }
    var mystring = JSON.stringify(myArray);
    //myString = "order: {currency: 'eur', order_items: " + myString + "}";
    console.log(myString);​
}

The way I get the data inside the order_items array is being cool, but when I try to concat the array with the rest (line in comments), I get:

?:??: W/?(?): Uncaught SyntaxError: Unexpected token ILLEGAL at file:///android_asset/www/november/js/t03/Booking/bookingProcess.js:96

Is there something I'm missing about the way a string and a JSON structure can be combined?

Thank you very much.

7
  • It’s difficult to understand the acutal question, please explain "concat the array with the rest (line in comments)". Commented Nov 20, 2012 at 10:00
  • Concatenating two valid JSON strings won't create a valid JSON strings (unless they both describe a number) Commented Nov 20, 2012 at 10:00
  • JSON.stringify(myArray) should always produce a valid JSON string unless the argument cannot be encoded. Commented Nov 20, 2012 at 10:04
  • 1
    Check variable name: mystring vs myString. Commented Nov 20, 2012 at 10:05
  • @YuryTarabanko you should write an answer Commented Nov 20, 2012 at 10:09

3 Answers 3

1

why don't you do it like this?

var x = {
    order: {
        currency: 'eur',
        order_items: []
    }
};
for (var i = 0; i < 10; i++) {
    var newObject = {
        i: i,
        Text: 'Some Text'
    };
    x.order.order_items.push(newObject);
}

var str = JSON.stringify(x);

look at this fiddle: http://jsfiddle.net/9V6Vb/

Here is the generated JSON:

{
    "order": {
        "currency": "eur",
        "order_items": [{
            "i": 0,
            "Text": "Some Text"},
        {
            "i": 1,
            "Text": "Some Text"},
        {
            "i": 2,
            "Text": "Some Text"},
        {
            "i": 3,
            "Text": "Some Text"},
        {
            "i": 4,
            "Text": "Some Text"},
        {
            "i": 5,
            "Text": "Some Text"},
        {
            "i": 6,
            "Text": "Some Text"},
        {
            "i": 7,
            "Text": "Some Text"},
        {
            "i": 8,
            "Text": "Some Text"},
        {
            "i": 9,
            "Text": "Some Text"}]
    }
}​
Sign up to request clarification or add additional context in comments.

3 Comments

Fixed the typo, but if I understand right, he is trying to do some string concatenation to get the final JSON representation. What he should do (in my opinion) is construct the full object and stringify that.
He definitely should. I even think he does.
This solution is exactly what I needed. Thanks a lot, @Raiden.
0

As Koen say it, you can make an object with first property is order and stringify all object in one time.

var myOrder = {
order: {
currency: 'eur',
         order_items: [
           {
             id: 3,
             quantity: 1
           },
           {
             id: 67,
             quantity: 1
           }
         ]
      }
}
console.log("currency : " + myOrder.order.currency);
for (var i in myOrder.order.order_items) {
    console.log("items number " + i);
    console.log("items id" + myOrder.order.order_items[i].id);
    console.log("items quantity" + myOrder.order.order_items[i].quantity);
}
var myString = JSON.stringify(myOrder);
console.log(myString);

but I think you should make "order" your object direcly

Regards,

Comments

0

JSON should be encapsulated in { and }.

So your JSON probably should be like:

{
    "order": {
        "currency": "eur",
        "order_items": [
            {
                "id": 3,
                "quantity": 1
            },
            {
                "id": 67,
                "quantity": 1
            }
        ]
    }
}

9 Comments

JSON.parse("[]") returns an array. JSON.parse("{a:1}") throws an exception.
@JanDvorak You're right, updated my answer so all properties are between quotes.
[1,2,3] is still valid JSON
JSON.parse('{"order": {"currency": "eur", "order_items": [{"id": 3,"quantity": 1},{"id": 67,"quantity": 1}]}}') is valid
As is JSON.parse('[1,2,3]'), so there need not be any curly brackets in valid JSON.
|

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.