You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Yury Luneff <bi...@ya.ru> on 2010/01/07 22:14:41 UTC
nested jsonobjects/arrays
Hello!
I have following code:
public String getRoutes() {
List<Route> routes = routeDAO.findAll();
JSONArray jsonRoutes = new JSONArray();
for (Route route : routes) {
JSONArray jsonRoutePoints = new JSONArray();
for (RoutePoint point : route.getPoints()) {
JSONObject jsonPoint = new JSONObject();
jsonPoint.put("latitude", point.getLatitude());
jsonPoint.put("longitude", point.getLongitude());
jsonRoutePoints.put(jsonPoint.toString());
}
jsonRoutes.put(jsonRoutePoints);
}
return jsonRoutes.toString();
}
Goal is to get a JSON object that is accesible like
"routes[0][0].latitude". But nested JSON objects are quoted by "\"" when the
jsonRoutes.toString() is called.
I receive an array of strings like "{"longitude":38.94441,"latitude":47.203542}"
for each jsonPoint variable instead of {"longitude":38.94441,"latitude":47.203542}
for each one.
This happens because JSONObject's toString doesn't treat its
nested instances the way it probably should do. If jsonobject has
nested object the whole nested object should not be escaped.
I guess the problem relies somewhere around
JSONObject.valueToString method.
Or am I using JSONObject/JSONArray in a wrong way?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: nested jsonobjects/arrays
Posted by Yury Luneff <bi...@ya.ru>.
wrong JSONArray :) sorry :)
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: nested jsonobjects/arrays
Posted by Yury Luneff <bi...@ya.ru>.
At first, I thought I embarassingly overlooked I was quoting the data
by myself, but nevertheless (strangely) I still get the same behaviour:
in the template:
var routes = ${routes};
in rendered page:
var routes = [["{\"longitude\":38.94441,\"latitude\":47.203542}",...
The corresponding code is now:
public String getRoutes() {
List<Route> routes = routeDAO.findAll();
JSONArray jsonRoutes = new JSONArray();
for (Route route : routes) {
JSONArray jsonRoutePoints = new JSONArray();
for (RoutePoint point : route.getPoints()) {
JSONObject jsonPoint = new JSONObject();
jsonPoint.put("latitude", point.getLatitude());
jsonPoint.put("longitude", point.getLongitude());
jsonRoutePoints.put(jsonPoint);
}
jsonRoutes.put(jsonRoutePoints);
}
return jsonRoutes.toString();
}
> jsonRoutePoints.put(jsonPoint)
> 2010/1/7 Yury Luneff <bi...@ya.ru>:
>> Hello!
>>
>> I have following code:
>> public String getRoutes() {
>> List<Route> routes = routeDAO.findAll();
>> JSONArray jsonRoutes = new JSONArray();
>>
>> for (Route route : routes) {
>> JSONArray jsonRoutePoints = new JSONArray();
>> for (RoutePoint point : route.getPoints()) {
>> JSONObject jsonPoint = new JSONObject();
>> jsonPoint.put("latitude", point.getLatitude());
>> jsonPoint.put("longitude", point.getLongitude());
>> jsonRoutePoints.put(jsonPoint.toString());
>> }
>> jsonRoutes.put(jsonRoutePoints);
>> }
>> return jsonRoutes.toString();
>> }
>>
>> Goal is to get a JSON object that is accesible like
>> "routes[0][0].latitude". But nested JSON objects are quoted by "\"" when the
>> jsonRoutes.toString() is called.
>>
>> I receive an array of strings like "{"longitude":38.94441,"latitude":47.203542}"
>> for each jsonPoint variable instead of {"longitude":38.94441,"latitude":47.203542}
>> for each one.
>>
>> This happens because JSONObject's toString doesn't treat its
>> nested instances the way it probably should do. If jsonobject has
>> nested object the whole nested object should not be escaped.
>>
>> I guess the problem relies somewhere around
>> JSONObject.valueToString method.
>>
>> Or am I using JSONObject/JSONArray in a wrong way?
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
--
С уважением,
Yury mailto:bitterman@ya.ru
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: nested jsonobjects/arrays
Posted by Howard Lewis Ship <hl...@gmail.com>.
jsonRoutePoints.put(jsonPoint)
2010/1/7 Yury Luneff <bi...@ya.ru>:
> Hello!
>
> I have following code:
> public String getRoutes() {
> List<Route> routes = routeDAO.findAll();
> JSONArray jsonRoutes = new JSONArray();
>
> for (Route route : routes) {
> JSONArray jsonRoutePoints = new JSONArray();
> for (RoutePoint point : route.getPoints()) {
> JSONObject jsonPoint = new JSONObject();
> jsonPoint.put("latitude", point.getLatitude());
> jsonPoint.put("longitude", point.getLongitude());
> jsonRoutePoints.put(jsonPoint.toString());
> }
> jsonRoutes.put(jsonRoutePoints);
> }
> return jsonRoutes.toString();
> }
>
> Goal is to get a JSON object that is accesible like
> "routes[0][0].latitude". But nested JSON objects are quoted by "\"" when the
> jsonRoutes.toString() is called.
>
> I receive an array of strings like "{"longitude":38.94441,"latitude":47.203542}"
> for each jsonPoint variable instead of {"longitude":38.94441,"latitude":47.203542}
> for each one.
>
> This happens because JSONObject's toString doesn't treat its
> nested instances the way it probably should do. If jsonobject has
> nested object the whole nested object should not be escaped.
>
> I guess the problem relies somewhere around
> JSONObject.valueToString method.
>
> Or am I using JSONObject/JSONArray in a wrong way?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
--
Howard M. Lewis Ship
Creator of Apache Tapestry
The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!
(971) 678-5210
http://howardlewisship.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org