Java 获取JSON数组的元素

Java 获取JSON数组的元素,java,android,json,Java,Android,Json,我有一个JSON文件,如下所示: {"posts":[{"Latitude":"53.38246685","lontitude":"-6.41501535"}, {"Latitude":"53.4062787","lontitude":"-6.3767205"}]} JSONObject o = new JSONOb

我有一个JSON文件,如下所示:

{"posts":[{"Latitude":"53.38246685","lontitude":"-6.41501535"},
{"Latitude":"53.4062787","lontitude":"-6.3767205"}]}
JSONObject o = new JSONObject(s);
JSONArray a = o.getJSONArray("posts");
o = a.getJSONObject(0);
lat = (int) (o.getDouble("Latitude")* 1E6);
lng = (int) (o.getDouble("lontitude")* 1E6); 
我可以通过以下步骤得到第一组纬度和lontitude坐标:

{"posts":[{"Latitude":"53.38246685","lontitude":"-6.41501535"},
{"Latitude":"53.4062787","lontitude":"-6.3767205"}]}
JSONObject o = new JSONObject(s);
JSONArray a = o.getJSONArray("posts");
o = a.getJSONObject(0);
lat = (int) (o.getDouble("Latitude")* 1E6);
lng = (int) (o.getDouble("lontitude")* 1E6); 
有人知道如何获得所有纬度和lontitude值吗


任何帮助都将不胜感激。

创建
ArrayList
s以获取结果:

JSONObject o = new JSONObject(s);
JSONArray a = o.getJSONArray("posts");
int arrSize = a.length();
List<Integer> lat = new ArrayList<Integer>(arrSize);
List<Integer> lon = new ArrayList<Integer>(arrSize);
for (int i = 0; i < arrSize; ++i) {
    o = a.getJSONObject(i);
    lat.add((int) (o.getDouble("Latitude")* 1E6));
    lon.add((int) (o.getDouble("lontitude")* 1E6));
}
JSONObject o=新的JSONObject;
JSONArray a=o.getJSONArray(“posts”);
int arrSize=a.长度();
List lat=新阵列列表(阵列大小);
列表lon=新的ArrayList(arrSize);
对于(int i=0;i

这将覆盖任何数组大小,即使有两个以上的值。

相信我的内存和常识。。。您是否尝试过:

o = a.getJSONObject(1);

在下面的代码中,我使用Gson将JSON字符串转换为java对象,因为Gson可以使用对象定义直接创建所需类型的对象

String json_string  = {"posts":[{"Latitude":"53.38246685","lontitude":"-6.41501535"},{"Latitude":"53.4062787","lontitude":"-6.3767205"}]}

JsonObject out = new JsonObject();
out = new JsonParser().parse(json_string).getAsJsonObject();

JsonArray listJsonArray = new JsonArray();
listJsonArray = out.get("posts").getAsJsonArray();

Gson gson = new Gson();
Type listType = new TypeToken<Collection<Info>>() { }.getType();

private Collection<Info> infoList;
infoList = (Collection<Info>) gson.fromJson(listJsonArray, listType);
List<Info> result = new ArrayList<>(infoList);

Double lat,long;
if (result.size() > 0) {
            for (int j = 0; j < result.size(); j++) {
                lat = result.get(j).getLatitude();
                long = result.get(j).getlongitude();
            }

//Generic Class
public class Info {

    @SerializedName("Latitude")
    private Double Latitude;

    @SerializedName("longitude")
    private Double longitude;

    public Double getLatitude() {  return Latitude; }

    public Double getlongitude() {return longitude;}

    public void setMac(Double Latitude) {
         this.Latitude = Latitude;
    }

    public void setType(Double longitude) {
        this.longitude = longitude;
    }
 }
String json_String={“posts”:[{“Latitude”:“53.38246685”,“lontitude”:“-6.41501535”},{“Latitude”:“53.4062787”,“lontitude”:“-6.3767205”}
JsonObject out=新的JsonObject();
out=newJSONParser().parse(json_字符串).getAsJsonObject();
JsonArray listJsonArray=新的JsonArray();
listJsonArray=out.get(“posts”).getAsJsonArray();
Gson Gson=新的Gson();
类型listType=newTypeToken(){}.getType();
私人收藏信息列表;
infoList=(Collection)gson.fromJson(listJsonArray,listType);
列表结果=新的ArrayList(信息列表);
双板条,长;
如果(result.size()>0){
对于(int j=0;j

这里的结果是在lat和long变量中获得的。

这是一个更好的答案。我只是想让您注意索引参数。Cheers Binyamin Sharet,我尝试了您建议的方法,但没有添加数组大小,非常感谢:)JSONObject方法中的“s”是什么???
s
是要解析的JSON字符串。经度。除非您你不是控制数据的人。。。