0

I have a string compared to this:

{
    "objects": [{
        "originY": "top",
        "left": 0,
        "top": 0,
        "width": 118.33,
        "height": 100,
        "name": 1
    }, {
        "originY": "top",
        "left": 0,
        "top": 0,
        "width": 118.33,
        "height": 100,
        "name": 2
    }],
    "background": ""
}

I need to loop to this string and retrieve the values of left, top, width and height and multiply them by a factor and then save them as a new string again.

Any idea of how I could accomplish this?

Pragmateek
  • 13,174
  • 9
  • 74
  • 108
gco
  • 1,670
  • 7
  • 24
  • 46
  • 3
    This looks a lot like JSON... Parse it, change it, stringify it. – Denys Séguret May 12 '14 at 13:39
  • 1
    have to attempted to do it? – Ejaz May 12 '14 at 13:39
  • dystroy you're right. It is a stringified JSON of a canvas, and I need to replace the values inside. – gco May 12 '14 at 13:40
  • 1
    Why is it stringified? It's infinitely easier to modify it in it's original form, as an object. – Cerbrus May 12 '14 at 13:40
  • http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – Gavriel May 12 '14 at 13:41
  • 1
    @gco: JSON is a string. What you have is JSON. It's a stringified object, not stringified JSON. And if it started as an object, just use it as an object then stringify the result if you must have JSON. – Matt Burland May 12 '14 at 13:43
  • ahh.. ok thanks @MattBurland. I have the saved JSON's inside a database. I retrieve the JSON, to display the objects on a new canvas... But therefore I need to scale the values, so they fit. – gco May 12 '14 at 13:45
  • 1
    Then what @dystroy said - use `JSON.parse` then manipulate the object then `JSON.stringify`. Don't waste you time trying to string manipulation just to avoid parsing. It isn't worth the effort. – Matt Burland May 12 '14 at 13:49

2 Answers2

2

Because the string is JSON, the easiest way to handle the data is to parse it into an array of objects, update the values, then output it as a string again.

// Parse
var container = JSON.parse(yourString);

// Get and update
var i, len, top, left, width, height;
len = container.objects.length;
for (i = 0; i < len; i++) {
    top = container.objects[i].top;
    left = container.objects[i].left;
    height= container.objects[i].height;
    width = container.objects[i].width;
    // * Save top, left, width somewhere. *
    // Multiply by some factor.
    container.objects[i].top *= factor;
    container.objects[i].left *= factor;
    container.objects[i].height *= factor;
    container.objects[i].width *= factor;
}

// Convert to string again.
theString = JSON.stringify(container);
dgvid
  • 26,293
  • 5
  • 40
  • 57
1

You can assign the json to a variable and then iterate it and do what ever you want

        var koko = {
            "objects": [{
                "originY": "top",
                "left": 1,
                "top": 0,
                "width": 118.33,
                "height": 100,
                "name": 1
            }, {
                "originY": "top",
                "left": 2,
                "top": 0,
                "width": 118.33,
                "height": 100,
                "name": 2
            }],
            "background": ""
        }



        for(var i=0;i<koko.objects.length;i++) { koko.objects[i].left = 10; }
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43