質問

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?

役に立ちましたか?

解決 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"]);
 }

他のヒント

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"))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top