简体   繁体   English

将Servlet传递的复杂对象存储到JavaScript中的数组中

[英]Storing complex objects passed by servlet into an array in javascript

I have a servlet that passes an ArrayList into a jsp page that receives it and places it in an Array, but i can't access the parameters inside Loc if i store them in an array like this: 我有一个Servlet,它将ArrayList传递到接收它的jsp页面中并将其放置在Array中,但是如果我将它们存储在这样的数组中,则无法访问Loc内部的参数:

var locations = new Array();
<c:forEach items="${photosName}" var="photo" varStatus="loop">
    locations["${loop.index}"] = "${photo}";
</c:forEach>

the variable photo contains attributes like latitude, longitude and accuracy that i wish to store in the locations array. 可变照片包含希望存储在locations数组中的诸如纬度,经度和准确性之类的属性。 But if i store them like this how can i access them in other parts of code. 但是,如果我像这样存储它们,该如何在代码的其他部分中访问它们。

Example of what i want to do: 我想做的例子:

locations[i].latitude
locations[i].longitude
locations[i].accuracy

Any help is welcome :) 欢迎任何帮助:)

Tried this now: 现在尝试了这个:

var locations = new Array();
<c:forEach items="${photosName}" var="photo" varStatus="loop">
    locations["${loop.index}"] = JSON.stringify("${photo}");
</c:forEach>
alert("latitude: "+locations[0].latitude+ " longitude: "+locations[0].longitude);

It printed latitude: undefined longitude: undefined. 它打印纬度:未定义经度:未定义。 Do i need to import something for the json.stringify to work? 我需要导入一些东西才能使json.stringify工作吗?

Photo object in jsp is represented by this class: jsp中的照片对象由此类表示:

@Entity
public class Loc implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long id;
    private double latitude;
    private double longitude;
    private long time;
    private double accuracy;
    private int processed;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public long getTime() {
        return this.time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "latitude: " + latitude + " longitude: " + longitude + "time: "
                + time;
    }

    public double getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(double accuracy) {
        this.accuracy = accuracy;
    }

    public int getProcessed() {
        return processed;
    }

    public void setProcessed(int processed) {
        this.processed = processed;
    }
}

Try locations["${loop.index}"] = {JSON.stringify("${photo}")}; 尝试locations["${loop.index}"] = {JSON.stringify("${photo}")}; . Then you can access the attributes as you wrote: 然后,您可以访问编写的属性:

locations[i].latitude
locations[i].longitude
locations[i].accuracy

Note: photo is a string representation of a JSONObject. 注意: photo是JSONObject的字符串表示形式。 Like this: 像这样:

JSONObject jsonPhoto = gson.toJson(photoObj) //photoObj is instance of your java photo object class
String photo = jsonPhoto.toString();

For converting an object to json object you can use GSON library. 要将对象转换为json对象,可以使用GSON库。 Here is link to a nice tutorial. 这里是一个不错的教程的链接

If you have list of Java phtoObjects you can do like this in your servlet: 如果您有Java phtoObjects列表, phtoObjects可以在servlet中执行以下操作:

<%@ page import="com.google.gson.Gson" %>   
<%GSON gson = new Gson();%>    
var photoArray = JSON.stringify(<%=gson.toJson(phtoObjects).toString()%>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM