[英]How can I create an array of comma separated arrays using java
I am trying to use the google maps API
to list multiple locations in a web page using my Java Spring MVC Web Application
. 我正在尝试使用
google maps API
使用Java Spring MVC Web Application
在网页中列出多个位置。 The locations are stored in my database. 这些位置存储在我的数据库中。 Now I am trying to return a list of locations in the following format:
现在,我尝试以以下格式返回位置列表:
['ABC', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
I need this format since the locations is called as follows in the script: 我需要这种格式,因为在脚本中按以下方式调用位置:
var locations = [
['ABC', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
];
What I am trying to do is to create this arrays using Java and then pass it to the UI. 我正在尝试使用Java创建此数组,然后将其传递给UI。 I am not sure how to create such an array structure using Java.
我不确定如何使用Java创建这样的数组结构。
the array contains items of different data types. 该数组包含不同数据类型的项目。 this creates a problem in statically typed languages like Java.
这会在诸如Java之类的静态类型语言中造成问题。 the array will have to be defined
Object[]
which is problematic since the compiler will not be able to help you populate the array properly. 必须将数组定义为
Object[]
,这是有问题的,因为编译器将无法帮助您正确填充数组。
You should design the API between the server and client to use array of Json objects. 您应该在服务器和客户端之间设计API以使用Json对象数组。 something like
就像是
[
{"Name": "Coogee Beach", "longitude":-33.923036, "latitude":151.259052},
{"Name": "Cronulla Beach", "longitude":-34.028249, "latitude":151.157507},
...
]
this is best represented in Java by creating a class with properties that match the JSON object: 在Java中,最好通过创建一个具有与JSON对象匹配的属性的类来表示:
public class Place {
public String name;
public double longitude;
public double latitude;
}
you should create an array (or List
) of PLace
s and you can use one of the many Jason libraries that map Java objects to-from Json objects. 您应该创建一个
PLace
的数组(或List
),并且可以使用许多Jason库之一来将Java对象映射到Json对象。
The design of returning lat/long in array doesn't look great, but here's the solution that should work. 返回纬度/经度数组的设计看起来不太好,但是这是应该起作用的解决方案。
Object[][] result = new Object[list.size()][3];
for (int i = 0; i < list.size; i++) {
result[i][0] = list[i].locName;
result[i][1] = list[i].latitude;
result[i][2] = list[i].longitude;
}
return result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.