Setting JSON key from a variable?

  • Follow


Hi,

I am trying to build a json object from a predefined variable. But the
variable name is being interpreted as a literal string rather than the
contents of the variable.

For example the following code displays "NONE" rather than the
contents of the variable "_nothingselected_":

<script>
var NONE="_nothingselected_";
var jsonOptions={ NONE : "Please Select" };
for(index in jsonOptions)
{
 alert(index);
}
</script>

What is the correct way to do this?

Thanks
0
Reply marksmith5555 (101) 7/28/2009 9:03:26 AM

On Jul 28, 11:03=A0am, Mark Smith <marksmith5...@jungle-monkey.com>
wrote:
> Hi,
>
> I am trying to build a json object from a predefined variable. But the
> variable name is being interpreted as a literal string rather than the
> contents of the variable.
>
> For example the following code displays "NONE" rather than the
> contents of the variable "_nothingselected_":
>
> <script>
> var NONE=3D"_nothingselected_";
> var jsonOptions=3D{ NONE : "Please Select" };
> for(index in jsonOptions)
> {
> =A0alert(index);}
>
> </script>
>
> What is the correct way to do this?

var jsonOptions=3D {};
jsonOptions[NONE]=3D "Please Select";

--
Jorge
0
Reply Jorge 7/28/2009 9:09:49 AM


Mark Smith wrote:
> I am trying to build a json object from a predefined variable. But the
> variable name is being interpreted as a literal string rather than the
> contents of the variable.
>
> For example the following code displays "NONE" rather than the
> contents of the variable "_nothingselected_":
> 
> <script>

  <script type="text/javascript">

> var NONE="_nothingselected_";
> var jsonOptions={ NONE : "Please Select" };
> for(index in jsonOptions)
> {
>  alert(index);
> }
> </script>
> 
> What is the correct way to do this?

 var jsonOptions = {};
 jsonOptions[NONE] = "Please Select";

Asked at least a hundred times already, and well-documented.  RTFM, RTFFAQ,
STFW.

<http://jibbering.com/faq/#posting>

See also: <https://developer.mozilla.org/En/Using_native_JSON>.


PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
0
Reply Thomas 7/28/2009 9:18:42 AM

On Jul 28, 11:03=A0am, Mark Smith <marksmith5...@jungle-monkey.com>
wrote:
> Hi,
>
> I am trying to build a json object from a predefined variable. (...)

BTW, JSON is a *text* format. There's no such thing as a JSON object :
data can be formatted as a JSON *text*.

http://json.org/
http://www.ietf.org/rfc/rfc4627.txt
--
Jorge.
0
Reply Jorge 7/28/2009 10:08:52 AM

On Jul 28, 12:08=A0pm, Jorge <jo...@jorgechamorro.com> wrote:
>
> data can be formatted as a JSON *text*.

Formatted, serialized, or the less pompous term chosen by Crockford:
stringify(ed) : see 15.12.3 in the ES5 draft:
http://www.ecma-international.org/publications/files/drafts/tc39-2009-025.p=
df

--
Jorge.
0
Reply Jorge 7/28/2009 10:47:33 AM

4 Replies
590 Views

(page loaded in 0.105 seconds)

Similiar Articles:











7/23/2012 7:09:00 AM


Reply: