将数据的Java列表转换为array的JSON数组

大家,早安。希望你一切都好。实际上,我想将包含数据的列表转换为这种形式的JSON表:

[[45.21406,5.74749,445,"2019-11-05T15:29:45Z"],
[45.21401,5.74752,470,"2019-11-05T15:29:46Z"],
[45.21397,5.74763,472,"2019-11-05T15:29:47Z"],
[45.21393,5.74789,471,"2019-11-05T15:29:48Z"],
[45.21389,5.74849,473,"2019-11-05T15:29:49Z"]
]

我的代码是下一个:

    activity.longitude=result.getLastLocation().getLongitude();

    activity.latitude=result.getLastLocation().getLatitude();

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    activity.dateFormated= formatter.format(new Date());

    activity.altitude=result.getLastLocation().getAltitude();

    activity.data.add(activity.longitude);
    activity.data.add(activity.latitude);
    activity.data.add(activity.dateFormated);
    activity.data.add(activity.altitude);

非常感谢您的帮助。

评论
  • 1种矜持
    1种矜持 回复

    您需要使用JSON库来简化此过程。这是一个简单的入门。

    https://github.com/stleary/JSON-java

  • 任性
    任性 回复

    so this can be done using a host of different libraries but one that could be particularly helpful is Gson. Unfortunately you have not posted your POJO that represented these values so it's difficult to help you further and give an example but this hsould help.