Question

I've recently found out that you can parse JSON files in photoshop using jamJSON

This is good news, but I've got a couple of stumbling blocks: For example, this is my JSON file

{
"YEAR" : {
  "longname" : "New Year"
 }
}

I can access it with

var jsObj = jamJSON.parse (jsonText, true);
alert (jsObj["YEAR"]["longname"]) // New Year

But since each file is going to be different and "YEAR" may be "FRUIT" or "GOLD" in another file. How do I access the data without knowing the first part of the object?

Était-ce utile?

La solution 3

Although the answers are above are correct, I was getting confused between object and arrays (Easily done. I'm an artist, me) and was finally able to access the data using

 var jsObj = jamJSON.parse (jsonText, true);

  for (var key in jsObj) 
 {
   var obj = jsObj[key];
   alert (obj["longname"]);
 }

Autres conseils

Assuming your JSON file will always have a single entry with this format, you can indicate the index-position of the entry rather than the name. For example:

alert (jsObj["YEAR"]["longname"]) // New Year

is equivalent to:

alert (jsObj[0]["longname"]) // New Year

Best, use JSONPath JS, easy to use and fast as well. You can access the value without knowing the key name by this:

alert(jsonPath(jsObj, "$..longname"))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top