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

Generating Json

New Here ,
Mar 08, 2013 Mar 08, 2013

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

460
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

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

Translate
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);

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

Thanks that was it.  Much appreciated!!!

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

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
Resources