0

I use a GUI to create my page, it is saved as a JSON file, then the JSON file is read by a CMS.

The problem: I would like to tinker with the objects I created, but the code is super minimized, an I cannot find the object.

To be clear with an example. I create a page with a button named "BUTTON". It exports in a JSON like this:

"S":[  
     {  
        "Y":"h",
        "b":1,
        "nm":"BUTTON1",
        "f":"1,0,1,0",
        "c":"FFFFFF,000000",
        "l":"2020,1840,4140,8820",
        "n":"Helvetica",
        "h":192,
        "L":0,
        "i":0,
        "j":1,
        "vj":1,
        "t":"Button",
        "Act":[  
           {  
              "e":"OnClick",
              "s":"\r\ngraphicReplace(\"Public/essai/vue0\");"
           }
        ]
     }
 ]

And it displays a button in the CMS. But, if I try something like "alert(S.t);", I get a "ReferenceError: S is not defined".

What I would like to do, is find a way to say: Find me the object, for which one of the properties is equal to "Button". Give me is name.

So that I can, using JS, change its position, size, and so on.


Full JSON

{  
   "PG":{  
  "v":5,
  "h":20480,
  "w":25600,
  "pc":"FFFFFF",
  "de":"",
  "st":0,
  "Act":[  

  ],
  "S":[  
     {  
        "Y":"h",
        "b":1,
        "nm":"BUTTON1",
        "f":"1,0,1,0",
        "c":"FFFFFF,000000",
        "l":"2020,1840,4140,8820",
        "n":"Helvetica",
        "h":192,
        "L":0,
        "i":0,
        "j":1,
        "vj":1,
        "t":"Button",
        "Act":[  
           {  
              "e":"OnClick",
              "s":"\r\ngraphicReplace(\"Public/essai/vue0\");"
           }
        ]
     }
  ]

} }

1
  • It looks like it's S[0].t. Commented Jun 10, 2015 at 11:20

2 Answers 2

1

Your JSON holds an array which in turn contains the rest of the keys. You can see that after the 'S' key, brackets [] follow indicating just that.

So you(probably) need to use ..S[0].t

You can access arrays by their index like so [0] and keys with their 'names', like so '.Act'.

From what I see up there it should be json.PG.S[0].t where json means the name of the variable that actually contains that JSON,

var json = {
  "PG": {
    "v": 5,
    "h": 20480,
    "w": 25600,
    "pc": "FFFFFF",
    "de": "",
    "st": 0,
    "Act": [

    ],
    "S": [{
      "Y": "h",
      "b": 1,
      "nm": "BUTTON1",
      "f": "1,0,1,0",
      "c": "FFFFFF,000000",
      "l": "2020,1840,4140,8820",
      "n": "Helvetica",
      "h": 192,
      "L": 0,
      "i": 0,
      "j": 1,
      "vj": 1,
      "t": "Button",
      "Act": [{
        "e": "OnClick",
        "s": "\r\ngraphicReplace(\"Public/essai/vue0\");"
      }]
    }]
  }
}


alert(json.PG.S[0].t);

e.g assuming:

var json = {  
   "PG":{  
  "v":5,
  "h":20480,
  "w":25600,
  "pc":"FFFFFF",
  "de":"",
  "st":0,
  "Act":[  

  ],
  "S":[  
     {  
        "Y":"h",
        "b":1,
        "nm":"BUTTON1",
        "f":"1,0,1,0",
        "c":"FFFFFF,000000",
        "l":"2020,1840,4140,8820",
        "n":"Helvetica",
        "h":192,
        "L":0,
        "i":0,
        "j":1,
        "vj":1,
        "t":"Button",
        "Act":[  
           {  
              "e":"OnClick",
              "s":"\r\ngraphicReplace(\"Public/essai/vue0\");"
           }
        ]
     }
  ]
} }
Sign up to request clarification or add additional context in comments.

8 Comments

It's correct, but doesn't seem to be the case. S is not defined anyway.
Thank you for your help. Indeed, I have tried this, and the result is inconclusive alert(S[0].t) VM2035:2 Uncaught ReferenceError: S is not defined
that's because you posted an incomplete JSON to begin with
I did not think it was necessary to have the whole thing. I'll edit my first post to attach it all.
Thanks for your detailed answer. Unfortunately, I do not know the name of the JSON variable, with turns my original question in "How can I find the json variable name?"
|
0

you Json should be like

var S = [
     {
         "Y": "h",
         "b": 1,
         "nm": "BUTTON1",
         "f": "1,0,1,0",
         "c": "FFFFFF,000000",
         "l": "2020,1840,4140,8820",
         "n": "Helvetica",
         "h": 192,
         "L": 0,
         "i": 0,
         "j": 1,
         "vj": 1,
         "t": "Button",
         "Act": [
            {
                "e": "OnClick",
                "s": "\r\ngraphicReplace(\"Public/essai/vue0\");"
            }
         ]
     }
]  

or

var obj = {
    "S": [
         {
             "Y": "h",
             "b": 1,
             "nm": "BUTTON1",
             "f": "1,0,1,0",
             "c": "FFFFFF,000000",
             "l": "2020,1840,4140,8820",
             "n": "Helvetica",
             "h": 192,
             "L": 0,
             "i": 0,
             "j": 1,
             "vj": 1,
             "t": "Button",
             "Act": [
                {
                    "e": "OnClick",
                    "s": "\r\ngraphicReplace(\"Public/essai/vue0\");"
                }
             ]
         }
    ]
}  
alert(S[0].t);  // if you choose first
alert(obj.S[0].t);  // if you choose second

5 Comments

It's being returned like that by a CMS - I think he has trouble assigning it to a variable or something like that
Unfortunately, I do not make the JSON. It is created by the GUI, and read by the CMS. The very first character of it is a {, without any notion of variable :(
@MaximeVisconte did you actually JSON.parse()'d it first? - You have to parse it and assign it to a variable to access it properly
@Maxime Visconte assign it to some variable say var obj now alert(obj.S[0].t)
I assume the CMS does it, since the object described in the document is displayed on the screen. Is that a bad assumption?

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.