You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Leon Derks <le...@cumquat.nl> on 2008/05/15 11:22:46 UTC

T5 + JSON + key name

Hello

I have a  JSON object like this:

 JSONObject obj = new JSONObject();

obj.put(entity.getId(), entity.getName());

But how can I get the value from my json object in javascript, if I 
don't know the key name?

Is there a way to get the key name(s) from a json object in javascript?

Leon

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Sven Homburg <ho...@googlemail.com>.
i have not tried before:

var myHash = $H(transport.responseText)
var keyArray = myHash.keys()


2008/5/15 Leon Derks <le...@cumquat.nl>:

> Hi Sven,
>
> I only see that they get the value by using the key name.
> For example this:
>
> |var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
> data.name; //prints Violet.
> |
>
> But in my case, I don't know the key name, so I can't get the value.
> Is there a way to get the key names of a JSON Object?
> For example something like this:
> |
> data.keys; //returns {name, occupation}|
>
> |
> |
>
> Leon
>
>
>
> Sven Homburg wrote:
>
>> http://www.prototypejs.org/learn/json
>>
>> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>>
>>
>>
>>> Hello
>>>
>>> I have a  JSON object like this:
>>>
>>> JSONObject obj = new JSONObject();
>>>
>>> obj.put(entity.getId(), entity.getName());
>>>
>>> But how can I get the value from my json object in javascript, if I don't
>>> know the key name?
>>>
>>> Is there a way to get the key name(s) from a json object in javascript?
>>>
>>> Leon
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
with regards
Sven Homburg
http://tapestry5-components.googlecode.com

Re: T5 + JSON + key name

Posted by Josh Canfield <jo...@thedailytube.com>.
>   for(var key in jsonObject) {
>       addSelectOption(groupSelect, jsonObject[key], key, false);

jsonObject is a hash, the keys in the hash are not stored with a
predictable ordering. If you want an ordered list then you should
create an array.

On Thu, May 15, 2008 at 3:58 AM, Leon Derks <le...@cumquat.nl> wrote:
> Thanks Chris,
>
> That worked.
> The only thing is that the order of groups in the select is gone.
> I know for sure I put them in the correct order in the java jsonObject.
> I think the var jsonObject = response.evalJSON(); or the for(var key in
> jsonObject) messes up the order.
> Is there a way to solve this?
>
> For the ones interested, I fill my select box now in this way:
>
> Java Page:
> @OnEvent(component = "productSelect", value = "change")
>   JSONObject onProductSelectChanged(String value) {
>       ProductCategory category =
> ProductCategory.getProductCategoryByName(value);
>       groups = characteristicDAO.findGroupsByProductCategory(category);
>       JSONObject jsonObject = new JSONObject();
>       for (CharacteristicGroup group : groups) {
>           Long id = group.getId();
>           jsonObject.put(id.toString(), group.getName());
>       }
>       return jsonObject;
>   }
>
> javascript:
> function productSelectChanged(response) {
>   var groupSelect = $('groupSelect');
>   clearSelectOption(groupSelect);
>     var jsonObject = response.evalJSON();
>   for(var key in jsonObject) {
>       addSelectOption(groupSelect, jsonObject[key], key, false);
>   }
> }
>
> Chris Lewis wrote:
>>
>> Try:
>>
>> for (var i in object) {}
>>
>> Where object would be your json object.
>>
>> Leon Derks wrote:
>>
>>>
>>> Hi Sven,
>>>
>>> I only see that they get the value by using the key name.
>>> For example this:
>>>
>>> |var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
>>> data.name; //prints Violet.
>>> |
>>>
>>> But in my case, I don't know the key name, so I can't get the value.
>>> Is there a way to get the key names of a JSON Object?
>>> For example something like this:
>>> |
>>> data.keys; //returns {name, occupation}|
>>>
>>> |
>>> |
>>>
>>> Leon
>>>
>>>
>>> Sven Homburg wrote:
>>>
>>>>
>>>> http://www.prototypejs.org/learn/json
>>>>
>>>> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>>>>
>>>>
>>>>>
>>>>> Hello
>>>>>
>>>>> I have a  JSON object like this:
>>>>>
>>>>> JSONObject obj = new JSONObject();
>>>>>
>>>>> obj.put(entity.getId(), entity.getName());
>>>>>
>>>>> But how can I get the value from my json object in javascript, if I
>>>>> don't
>>>>> know the key name?
>>>>>
>>>>> Is there a way to get the key name(s) from a json object in javascript?
>>>>>
>>>>> Leon
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Andreas Andreou <an...@gmail.com>.
You probably have to rethink your json structure - it doesn't look
like you're interested
in getting values from keys... you just want the values in the particular order.

Take a look at JSONArray

On Thu, May 15, 2008 at 2:19 PM, Leon Derks <le...@cumquat.nl> wrote:
> Hi Chris,
>
>  I just noticed that the options in the select are initialy not ordered.
>  But when I persist the options (groups) in my session data, the option are
> ordered after I have clicked the search button and submitted the form.
>  Then refreshed page, contains a select with ordered group options.
>
>  Do you have an explanation for this behaviour?
>
>  Leon
>
>
>
>
>  Leon Derks wrote:
>
> > Thanks Chris,
> >
> > That worked.
> > The only thing is that the order of groups in the select is gone.
> > I know for sure I put them in the correct order in the java jsonObject.
> > I think the var jsonObject = response.evalJSON(); or the for(var key in
> jsonObject) messes up the order.
> > Is there a way to solve this?
> >
> > For the ones interested, I fill my select box now in this way:
> >
> > Java Page:
> > @OnEvent(component = "productSelect", value = "change")
> >   JSONObject onProductSelectChanged(String value) {
> >       ProductCategory category =
> ProductCategory.getProductCategoryByName(value);
> >       groups = characteristicDAO.findGroupsByProductCategory(category);
> >       JSONObject jsonObject = new JSONObject();
> >       for (CharacteristicGroup group : groups) {
> >           Long id = group.getId();
> >           jsonObject.put(id.toString(), group.getName());
> >       }
> >       return jsonObject;
> >   }
> >
> > javascript:
> > function productSelectChanged(response) {
> >   var groupSelect = $('groupSelect');
> >   clearSelectOption(groupSelect);
> >     var jsonObject = response.evalJSON();
> >   for(var key in jsonObject) {
> >       addSelectOption(groupSelect, jsonObject[key], key, false);
> >   }
> > }
> >
> > Chris Lewis wrote:
> >
> > > Try:
> > >
> > > for (var i in object) {}
> > >
> > > Where object would be your json object.
> > >
> > > Leon Derks wrote:
> > >
> > >
> > > > Hi Sven,
> > > >
> > > > I only see that they get the value by using the key name.
> > > > For example this:
> > > >
> > > > |var data = '{ "name": "Violet", "occupation": "character"
> }'.evalJSON();
> > > > data.name; //prints Violet.
> > > > |
> > > >
> > > > But in my case, I don't know the key name, so I can't get the value.
> > > > Is there a way to get the key names of a JSON Object?
> > > > For example something like this:
> > > > |
> > > > data.keys; //returns {name, occupation}|
> > > >
> > > > |
> > > > |
> > > >
> > > > Leon
> > > >
> > > >
> > > > Sven Homburg wrote:
> > > >
> > > >
> > > > > http://www.prototypejs.org/learn/json
> > > > >
> > > > > 2008/5/15 Leon Derks <le...@cumquat.nl>:
> > > > >
> > > > >
> > > > >
> > > > > > Hello
> > > > > >
> > > > > > I have a  JSON object like this:
> > > > > >
> > > > > > JSONObject obj = new JSONObject();
> > > > > >
> > > > > > obj.put(entity.getId(), entity.getName());
> > > > > >
> > > > > > But how can I get the value from my json object in javascript, if
> I
> > > > > > don't
> > > > > > know the key name?
> > > > > >
> > > > > > Is there a way to get the key name(s) from a json object in
> javascript?
> > > > > >
> > > > > > Leon
> > > > > >
> > > > > >
> ---------------------------------------------------------------------
> > > > > > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > > > > > For additional commands, e-mail: users-help@tapestry.apache.org
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > > > For additional commands, e-mail: users-help@tapestry.apache.org
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
> >
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>  For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Andreas Andreou - andyhot@apache.org - http://blog.andyhot.gr
Tapestry / Tacos developer
Open Source / JEE Consulting

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Leon Derks <le...@cumquat.nl>.
Hi Chris,

I just noticed that the options in the select are initialy not ordered.
But when I persist the options (groups) in my session data, the option 
are ordered after I have clicked the search button and submitted the form.
Then refreshed page, contains a select with ordered group options.

Do you have an explanation for this behaviour?

Leon


Leon Derks wrote:
> Thanks Chris,
>
> That worked.
> The only thing is that the order of groups in the select is gone.
> I know for sure I put them in the correct order in the java jsonObject.
> I think the var jsonObject = response.evalJSON(); or the for(var key 
> in jsonObject) messes up the order.
> Is there a way to solve this?
>
> For the ones interested, I fill my select box now in this way:
>
> Java Page:
> @OnEvent(component = "productSelect", value = "change")
>    JSONObject onProductSelectChanged(String value) {
>        ProductCategory category = 
> ProductCategory.getProductCategoryByName(value);
>        groups = characteristicDAO.findGroupsByProductCategory(category);
>        JSONObject jsonObject = new JSONObject();
>        for (CharacteristicGroup group : groups) {
>            Long id = group.getId();
>            jsonObject.put(id.toString(), group.getName());
>        }
>        return jsonObject;
>    }
>
> javascript:
> function productSelectChanged(response) {
>    var groupSelect = $('groupSelect');
>    clearSelectOption(groupSelect);
>      var jsonObject = response.evalJSON();
>    for(var key in jsonObject) {
>        addSelectOption(groupSelect, jsonObject[key], key, false);
>    }
> }
>
> Chris Lewis wrote:
>> Try:
>>
>> for (var i in object) {}
>>
>> Where object would be your json object.
>>
>> Leon Derks wrote:
>>  
>>> Hi Sven,
>>>
>>> I only see that they get the value by using the key name.
>>> For example this:
>>>
>>> |var data = '{ "name": "Violet", "occupation": "character" 
>>> }'.evalJSON();
>>> data.name; //prints Violet.
>>> |
>>>
>>> But in my case, I don't know the key name, so I can't get the value.
>>> Is there a way to get the key names of a JSON Object?
>>> For example something like this:
>>> |
>>> data.keys; //returns {name, occupation}|
>>>
>>> |
>>> |
>>>
>>> Leon
>>>
>>>
>>> Sven Homburg wrote:
>>>    
>>>> http://www.prototypejs.org/learn/json
>>>>
>>>> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>>>>
>>>>  
>>>>      
>>>>> Hello
>>>>>
>>>>> I have a  JSON object like this:
>>>>>
>>>>> JSONObject obj = new JSONObject();
>>>>>
>>>>> obj.put(entity.getId(), entity.getName());
>>>>>
>>>>> But how can I get the value from my json object in javascript, if I
>>>>> don't
>>>>> know the key name?
>>>>>
>>>>> Is there a way to get the key name(s) from a json object in 
>>>>> javascript?
>>>>>
>>>>> Leon
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>>
>>>>>
>>>>>             
>>>>         
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>     
>>
>>   
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Leon Derks <le...@cumquat.nl>.
Thanks Chris,

That worked.
The only thing is that the order of groups in the select is gone.
I know for sure I put them in the correct order in the java jsonObject.
I think the var jsonObject = response.evalJSON(); or the for(var key in 
jsonObject) messes up the order.
Is there a way to solve this?

For the ones interested, I fill my select box now in this way:

Java Page:
@OnEvent(component = "productSelect", value = "change")
    JSONObject onProductSelectChanged(String value) {
        ProductCategory category = 
ProductCategory.getProductCategoryByName(value);
        groups = characteristicDAO.findGroupsByProductCategory(category);
        JSONObject jsonObject = new JSONObject();
        for (CharacteristicGroup group : groups) {
            Long id = group.getId();
            jsonObject.put(id.toString(), group.getName());
        }
        return jsonObject;
    }

javascript:
function productSelectChanged(response) {
    var groupSelect = $('groupSelect');
    clearSelectOption(groupSelect);
   
    var jsonObject = response.evalJSON();
    for(var key in jsonObject) {
        addSelectOption(groupSelect, jsonObject[key], key, false);
    }
}

Chris Lewis wrote:
> Try:
>
> for (var i in object) {}
>
> Where object would be your json object.
>
> Leon Derks wrote:
>   
>> Hi Sven,
>>
>> I only see that they get the value by using the key name.
>> For example this:
>>
>> |var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
>> data.name; //prints Violet.
>> |
>>
>> But in my case, I don't know the key name, so I can't get the value.
>> Is there a way to get the key names of a JSON Object?
>> For example something like this:
>> |
>> data.keys; //returns {name, occupation}|
>>
>> |
>> |
>>
>> Leon
>>
>>
>> Sven Homburg wrote:
>>     
>>> http://www.prototypejs.org/learn/json
>>>
>>> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>>>
>>>  
>>>       
>>>> Hello
>>>>
>>>> I have a  JSON object like this:
>>>>
>>>> JSONObject obj = new JSONObject();
>>>>
>>>> obj.put(entity.getId(), entity.getName());
>>>>
>>>> But how can I get the value from my json object in javascript, if I
>>>> don't
>>>> know the key name?
>>>>
>>>> Is there a way to get the key name(s) from a json object in javascript?
>>>>
>>>> Leon
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>>
>>>>
>>>>     
>>>>         
>>>   
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>     
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Chris Lewis <ch...@bellsouth.net>.
Try:

for (var i in object) {}

Where object would be your json object.

Leon Derks wrote:
> Hi Sven,
>
> I only see that they get the value by using the key name.
> For example this:
>
> |var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
> data.name; //prints Violet.
> |
>
> But in my case, I don't know the key name, so I can't get the value.
> Is there a way to get the key names of a JSON Object?
> For example something like this:
> |
> data.keys; //returns {name, occupation}|
>
> |
> |
>
> Leon
>
>
> Sven Homburg wrote:
>> http://www.prototypejs.org/learn/json
>>
>> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>>
>>  
>>> Hello
>>>
>>> I have a  JSON object like this:
>>>
>>> JSONObject obj = new JSONObject();
>>>
>>> obj.put(entity.getId(), entity.getName());
>>>
>>> But how can I get the value from my json object in javascript, if I
>>> don't
>>> know the key name?
>>>
>>> Is there a way to get the key name(s) from a json object in javascript?
>>>
>>> Leon
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>     
>>
>>
>>   
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

-- 
http://thegodcode.net


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Leon Derks <le...@cumquat.nl>.
Hi Sven,

I only see that they get the value by using the key name.
For example this:

|var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
data.name; //prints Violet.
|

But in my case, I don't know the key name, so I can't get the value.
Is there a way to get the key names of a JSON Object?
For example something like this:
|
data.keys; //returns {name, occupation}|

|
|

Leon


Sven Homburg wrote:
> http://www.prototypejs.org/learn/json
>
> 2008/5/15 Leon Derks <le...@cumquat.nl>:
>
>   
>> Hello
>>
>> I have a  JSON object like this:
>>
>> JSONObject obj = new JSONObject();
>>
>> obj.put(entity.getId(), entity.getName());
>>
>> But how can I get the value from my json object in javascript, if I don't
>> know the key name?
>>
>> Is there a way to get the key name(s) from a json object in javascript?
>>
>> Leon
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>     
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5 + JSON + key name

Posted by Sven Homburg <ho...@googlemail.com>.
http://www.prototypejs.org/learn/json

2008/5/15 Leon Derks <le...@cumquat.nl>:

> Hello
>
> I have a  JSON object like this:
>
> JSONObject obj = new JSONObject();
>
> obj.put(entity.getId(), entity.getName());
>
> But how can I get the value from my json object in javascript, if I don't
> know the key name?
>
> Is there a way to get the key name(s) from a json object in javascript?
>
> Leon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
with regards
Sven Homburg
http://tapestry5-components.googlecode.com