如何解析没有键的对象

时间:2018-10-28 19:20:54

标签: java hibernate

我有以下查询:

public List<Object> findNearbyGangs(double lat, double lng, double distance) {
    Query query = this.entityManager.createNativeQuery("SELECT id, (6371 * acos (cos(radians(:latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians(:longitude))  + sin(radians(:latitude)) * sin(radians(latitude)))) AS distance FROM Gang g GROUP BY id HAVING distance < :distance ORDER BY distance")
        .setParameter("latitude", lat)
        .setParameter("longitude", lng)
        .setParameter("distance", distance);

    List<Object> objects = query.getResultList();
    return objects;
}

objects包含

[
    [
        3,
        0
    ],
    [
        321,
        0
    ],
    [
        353,
        1.3575295456440253
    ]
]

如何解析该对象,以使id = 3且距离= 0,id = 321且距离= 0,依此类推?

1 个答案:

答案 0 :(得分:0)

我知道了:

public List<NearbyGang> getNearbyGangs(Double longitude, Double latitude, int radius) {
        List<Object> nearbyGangsFromDatabase = this.gangRepositoryImpl.findNearbyGangs(longitude, latitude, radius);
        List<NearbyGang> nearbyGangs = new ArrayList<NearbyGang>();
        for (Object object : nearbyGangsFromDatabase) {
            Class oClass = object.getClass();
            if (oClass.isArray()) {
                for (int i = 0; i < Array.getLength(object); i++) {
                    BigInteger gangId = (BigInteger) Array.get(object, 0);
                    Gang gang = this.getById(gangId.longValue()).get();

                    Double distance = (Double) Array.get(object, 1);

                    NearbyGang nearbyGang = new NearbyGang(gang, distance);
                    nearbyGangs.add(nearbyGang);
                }
            }
        }
        return nearbyGangs;
    }