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

Generating Json

New Here ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

I am trying to generate the following with Coldfusion to pass that as a json object to JS. 

[

  {

    key: "Final List",

    values: [

      {

        "label": "Title I",

        "value" : 33

      } ,

      {

        "label": "Title II",

        "value" : 33

      }

    ]

  }

I have:

postObject["KEY"] = "Final List";

postObject["VALUES"] = [];

postObject.values[1] = {'label':"Title I", 'value':"33"};

postObject.values[2] = {'label':"Title II", 'value':"35"};

serializeJSON(postObject);

The result I get is a bit different:

{"VALUES":[{"value":33,"label":"Title I"},{"value":35,"label":"Title II"},

"key":"Final List"}

Any help appreciated...

Views

407

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
community guidelines

correct answers 1 Correct answer

Explorer , Mar 08, 2013 Mar 08, 2013

Keep result(postObject) in an array. Javascript is case sensitive so its better to use structinsert in that case

resultArray = ArrayNew(1);

structInsert(postObject, "key", "Final List");

structInsert(postObject, "values", "");

TEMPvalues = ArrayNew(1);

TEMPvalues [1] = {'label':"Title I", 'value':"33"};

TEMPvalues [2] = {'label':"Title II", 'value':"35"};

postObject.values = TEMPvalues;

resultArray[1] =  postObject;

serializeJSON(resultArray);

Votes

Translate

Translate
Explorer ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

Keep result(postObject) in an array. Javascript is case sensitive so its better to use structinsert in that case

resultArray = ArrayNew(1);

structInsert(postObject, "key", "Final List");

structInsert(postObject, "values", "");

TEMPvalues = ArrayNew(1);

TEMPvalues [1] = {'label':"Title I", 'value':"33"};

TEMPvalues [2] = {'label':"Title II", 'value':"35"};

postObject.values = TEMPvalues;

resultArray[1] =  postObject;

serializeJSON(resultArray);

Votes

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
community guidelines
New Here ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

Thanks that was it.  Much appreciated!!!

Votes

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
community guidelines
Guide ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

LATEST

@NeoCold,

Alternatively, you could use the original syntax you posted, but put the structure key names in lowercase as you were intending:

postObject["key"] = "Final List";

postObject["values"] = [];

Since you are using the bracket notation for creating structure keys already, ColdFusion will respect the case of the keys you enter in the brackets.

-Carl V.

Votes

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
community guidelines
Resources
Documentation