Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Bug WRT Javascript JSON in Acrobat Pro DC

New Here ,
Sep 04, 2018 Sep 04, 2018

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

TOPICS
Acrobat SDK and JavaScript , Windows
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 04, 2018 Sep 04, 2018

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

Translate
Community Expert ,
Sep 04, 2018 Sep 04, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2018 Sep 04, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2018 Sep 04, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2018 Sep 04, 2018

Cool!! Must come with JS engine now. I can't see anyone at Adobe deliberately adding cool new features to JS  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2018 Sep 04, 2018

I believe that is the case but just for the Acrobat JavaScript side. It's still missing from the 3D JS engine.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2018 Sep 05, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2018 Sep 05, 2018

If you want to do that you should use a literal object, instead, like this:

var arr = {};

arr.myProp = "foo";

console.println(arr["myProp"]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2018 Sep 05, 2018

I do agree ! Was just restating the objectness of Javascript

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2018 Sep 05, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2018 Sep 05, 2018
LATEST

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.

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