1

我正在尝试将 json 导出到 Android 设备。我已经使用像 Play 之类的 java 完成了它!文档: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
      }
   ]
}
4

1 回答 1

0

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

return ok(restaurantes);
于 2014-10-03T09:55:11.953 回答