Bug WRT Javascript JSON in Acrobat Pro DC

New Here ,
Sep 04, 2018

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: []

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

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".

TOPICS
Acrobat SDK and JavaScript, Windows

Views

242

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Bug WRT Javascript JSON in Acrobat Pro DC

New Here ,
Sep 04, 2018

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: []

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

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".

TOPICS
Acrobat SDK and JavaScript, Windows

Views

243

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 04, 2018 0
Most Valuable Participant ,
Sep 04, 2018

Copy link to clipboard

Copied

Acrobat JavaScript does not include JSON. Did you install it on your own?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 04, 2018 0
Adobe Community Professional ,
Sep 04, 2018

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".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 04, 2018 1
Most Valuable Participant ,
Sep 04, 2018

Copy link to clipboard

Copied

Oh wow, I didn't realize that... Is that a new addition?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 04, 2018 0
Adobe Community Professional ,
Sep 04, 2018

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  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 04, 2018 1
Adobe Community Professional ,
Sep 04, 2018

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 04, 2018 0
Valorous Hero ,
Sep 05, 2018

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 05, 2018 0
Most Valuable Participant ,
Sep 05, 2018

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 05, 2018 0
Valorous Hero ,
Sep 05, 2018

Copy link to clipboard

Copied

I do agree ! Was just restating the objectness of Javascript

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 05, 2018 0
Valorous Hero ,
Sep 05, 2018

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 05, 2018 0
Adobe Community Professional ,
Sep 05, 2018

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 05, 2018 1