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

JSON to JSX

Enthusiast ,
Nov 16, 2018 Nov 16, 2018

Hi All,

New to JSON.

My request is no need to hard code in script (.jsx). For that am using JSON.

We need the "dropdownlist" list "Customer1", "Customer2", "Customer3" that should comes from JSON file.

Could you please help for this.

var w = new Window ("dialog");

var myDropdown = w.add ("dropdownlist", undefined, ["Customer1", "Customer2", "Customer3"]);

myDropdown.selection = 1;

w.show ();

Thanks in Advance

TOPICS
Scripting
2.3K
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 , Nov 16, 2018 Nov 16, 2018

You can use eval to convert a JSON string into a JS object as follows

var a = eval('([ "Customer1", "Customer2", "Customer3" ])')

var b = eval('({"name":"John","age":30,"cars":[ "Ford", "BMW", "Fiat" ]})')

So in your code you could read a file with JSON string, convert it into JS object as shown above and then use the object to populate your UI like below

var myDropdown = w.add ("dropdownlist", undefined, a);  

Using eval can be a security threat so you could use a JSON library to parse out the strin

...
Translate
Community Expert ,
Nov 16, 2018 Nov 16, 2018

You can use eval to convert a JSON string into a JS object as follows

var a = eval('([ "Customer1", "Customer2", "Customer3" ])')

var b = eval('({"name":"John","age":30,"cars":[ "Ford", "BMW", "Fiat" ]})')

So in your code you could read a file with JSON string, convert it into JS object as shown above and then use the object to populate your UI like below

var myDropdown = w.add ("dropdownlist", undefined, a);  

Using eval can be a security threat so you could use a JSON library to parse out the string for you as JSON. You could try something like

GitHub - douglascrockford/JSON-js: JSON in JavaScript

-Manan

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
Explorer ,
Sep 18, 2019 Sep 18, 2019
LATEST
Hello Manan Joshi,
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