Copy link to clipboard
Copied
Does anybody else see this same behavior?
Script:
var ary = [];
ary["sam"] = "test string";
console.println("The array value is " +ary["sam"]);
console.println("JSON says: " +JSON.stringify(ary));
Output:
The array value is test string
JSON says: []
Acrobat DC JavaScript includes the JSON object.
However, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...
var ary = [];
ary[0] = "test string";
... which creates an empty array and then inserts a value at index 0 or...
var obj = {};
obj["sam"] = "test string";
... which creates a JSON object and assigns the value "test string" to the property named "sam".
Copy link to clipboard
Copied
Does anybody else see this same behavior?
Script:
var ary = [];
ary["sam"] = "test string";
console.println("The array value is " +ary["sam"]);
console.println("JSON says: " +JSON.stringify(ary));
Output:
The array value is test string
JSON says: []
Acrobat DC JavaScript includes the JSON object.
However, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...
var ary = [];
ary[0] = "test string";
... which creates an empty array and then inserts a value at index 0 or...
var obj = {};
obj["sam"] = "test string";
... which creates a JSON object and assigns the value "test string" to the property named "sam".
Copy link to clipboard
Copied
Acrobat JavaScript does not include JSON. Did you install it on your own?
Copy link to clipboard
Copied
Acrobat DC JavaScript includes the JSON object.
However, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...
var ary = [];
ary[0] = "test string";
... which creates an empty array and then inserts a value at index 0 or...
var obj = {};
obj["sam"] = "test string";
... which creates a JSON object and assigns the value "test string" to the property named "sam".
Copy link to clipboard
Copied
Oh wow, I didn't realize that... Is that a new addition?
Copy link to clipboard
Copied
Cool!! Must come with JS engine now. I can't see anyone at Adobe deliberately adding cool new features to JS
Copy link to clipboard
Copied
I believe that is the case but just for the Acrobat JavaScript side. It's still missing from the 3D JS engine.
Copy link to clipboard
Copied
however, your JSON code is incorrect. In JavaScript you can only add elements to an array using an index if the index is an integer. You either want...
technically speaking array are objects and object can have properties added.
So
var arr = [];
arr.myProp = "foo";
is certainly doable yet not really meaningful.
Copy link to clipboard
Copied
If you want to do that you should use a literal object, instead, like this:
var arr = {};
arr.myProp = "foo";
console.println(arr["myProp"]);
Copy link to clipboard
Copied
I do agree ! Was just restating the objectness of Javascript
Copy link to clipboard
Copied
Plus I can remember some case where we use arrays for storing values through indeces and props per properties. Can't remember why my colleague did this at the time but we certainly did.
Copy link to clipboard
Copied
Yes... an array is an object in JavaScript... but in JSON, it's defined as "an ordered list of values" where the array begins with [ (left bracket) and ends with ] (right bracket) and values are separated by, (comma). Because JSON is a lightweight data-interchange format, it needs to conform to the least common denominator for languages which means... unfortunately, you can't get too fancy with it.