Associative Array from Java Array

259 views
Skip to first unread message

Evan Kirkconnell

unread,
Jan 26, 2009, 11:37:19 AM1/26/09
to json-...@googlegroups.com
Hey All,
I like this library, but it doesn't seem to support what I was hoping to first use it for.  What I was wanting to do was specify a Java array, and tell it they member that should be the key values in the JSON and a different member that should be the values of the JSON.

So, basically I have this in Java:
new MyObject[]{new MyObject("key1", "val1"), new MyObject("key2", "val2")};

I'm able to produce this:
<script>
    [{"key1":"val1"},{"key2":"val2"}]
</script>
using this JSP:
    <json:array name="myAscObj" var="obj" items="${myObject}">
        <json:object>
            <json:property name="${obj.key}" value="${obj.value}"/>       
        </json:object>
    </json:array>

but what I'd really like to get is this:
<script>
{
    "key1":"val1",
    "key2":"val2"
}
</script>

by doing this:
    <json:array name="myAscObj" var="obj" items="${myObject}">
          <json:property name="${obj.key}" value="${obj.value}"/>       
    </json:array>


I thought for a sec that I could get close with this, but it didn't take the variable for the object name:

    <json:array name="myAscObj" var="obj" items="${myObject}">
        <json:object name="${obj.key}">
            <json:property name="value" value="${obj.value}"/>       
        </json:object>
    </json:array>

So, if anyone knows another way, let me know.  Otherwise, I guess it's just a request for an enhancement.
Thanks,
--Evan
   






James Wiltshire

unread,
Jan 27, 2009, 10:49:26 AM1/27/09
to json-taglib
Hi Evan
You could try something like this...

<json:object>
<c:forEach var="obj" items="myObject">
<json:property name="${obj.key}" value="${obj.value}"/>
</c:forEach>
</json:object>

Regards
James Wiltshire
Reply all
Reply to author
Forward
0 new messages