我无法使用Play framework 2.1提供JSON响应

时间:2013-06-21 04:22:07

标签: java json playframework-2.1

我正在尝试将json导出到Android设备。我用像Play这样的java做过! doc:http://www.playframework.com/documentation/2.1.1/JavaJsonRequests但它不起作用。我很害羞这不是一个Android问题,因为我对php return json_encode($some)做了同样的事情并且效果很好。我该怎么办?

package controllers;

import java.util.List;

import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import models.Restaurante;
import play.libs.Json;
import play.mvc.*;

public class Servicio extends Controller {  

public static Result coordenadas() {

    //System.out.println("XXXxxXX");

    ObjectNode rpta = Json.newObject();
    ArrayNode restaurantes = rpta.arrayNode();

    List <Restaurante> rests = Restaurante.allRests();
    Restaurante rest;

    for(int i=0; i<rests.size(); i++) {
        rest = rests.get(i);
        ObjectNode restJs = Json.newObject();
        restJs.put("nombre", rest.nombre);
        restJs.put("lon", rest.longitud);
        restJs.put("lat", rest.latitud);
        restaurantes.add(restJs);
    }

    ObjectNode response = Json.newObject();
    response.put("restaurantes", restaurantes);

    return ok(response);
}

}

我是从db获得的,浏览器显示了这个:

{
   "restaurantes":[
      {
         "nombre":"ODONTOLOGIA",
         "lon":-12.054695,
         "lat":-77.085268
      },
      {
         "nombre":"LETRAS",
         "lon":-12.057444,
         "lat":-77.081856
      },
      {
         "nombre":"FISICA",
         "lon":-12.060279,
         "lat":-77.081594
      },
      {
         "nombre":"COMEDOR",
         "lon":-12.059332,
         "lat":-77.083104
      },
      {
         "nombre":"QUIMICA",
         "lon":-12.06009,
         "lat":-77.083804
      },
      {
         "nombre":"CIBERCAFE",
         "lon":-12.059857,
         "lat":-77.084563
      },
      {
         "nombre":"GEOGRAFICA",
         "lon":-12.05507,
         "lat":-77.086127
      },
      {
         "nombre":"MECANICA",
         "lon":-12.055854,
         "lat":-77.081816
      },
      {
         "nombre":"ELECTRONICA",
         "lon":-12.056347,
         "lat":-77.082055
      },
      {
         "nombre":"ECONOMIA",
         "lon":-12.058123,
         "lat":-77.080639
      },
      {
         "nombre":"MINAS",
         "lon":-12.05199,
         "lat":-77.087165
      },
      {
         "nombre":"ADMINISTRACION",
         "lon":-12.05789,
         "lat":-77.081602
      },
      {
         "nombre":"Nuevo restaurante",
         "lon":-70.23123,
         "lat":-12.31234
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

尝试仅将餐馆作为ok属性返回:

return ok(restaurantes);