如何将JSON对象列表转换为JsonArray,然后对其进行迭代?

36

我试图将一个字符串(每个JSON对象都用逗号分隔,但第一个JSON对象是该集合的描述)与JSON对象转换为JsonArray,然后尝试对每个对象的某个子元素进行迭代JsonElement,但是每次尝试都以一个错误。

字符串示例:

{"type":"FeatureCollection","metadata":{"generated":1554314439000,"url":"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake","title":"USGS Earthquakes","status":200,"api":"1.7.0","count":8970},"features":[{"type":"Feature","properties":{"mag":2.2,"place":"84km SSE of Old Iliamna, Alaska","time":1554313967537,"updated":1554314345998,"tz":-540,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/ak0194a3ew0w","detail":"https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=ak0194a3ew0w&format=geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":74,"net":"ak","code":"0194a3ew0w","ids":",ak0194a3ew0w,","sources":",ak,","types":",geoserve,origin,","nst":null,"dmin":null,"rms":0.74,"gap":null,"magType":"ml","type":"earthquake","title":"M 2.2 - 84km SSE of Old Iliamna, Alaska"},"geometry":{"type":"Point","coordinates":[-154.542,59.0119,127.6]},"id":"ak0194a3ew0w"},

{"type":"Feature","properties":{"mag":1.1,"place":"107km W of Cantwell, Alaska","time":1554313769466,"updated":1554313953376,"tz":-540,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/ak0194a3e7ki","detail":"https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=ak0194a3e7ki&format=geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":19,"net":"ak","code":"0194a3e7ki","ids":",ak0194a3e7ki,","sources":",ak,","types":",geoserve,origin,","nst":null,"dmin":null,"rms":0.63,"gap":null,"magType":"ml","type":"earthquake","title":"M 1.1 - 107km W of Cantwell, Alaska"},"geometry":{"type":"Point","coordinates":[-151.0662,63.2378,7.8]},"id":"ak0194a3e7ki"},

我试过了:

JsonArray jsonObject = new JsonParser()
                    .parse(result)
                    .getAsJsonArray();
List<String> names = new ArrayList<>();
for (JsonElement jsonElement : jsonObject) {
    names.add(jsonElement.getAsJsonObject().get("properties").getAsString());
米恰尔·乔伯(MichałZiober)

insect.usgs.gov API以GeoJSON格式返回有效载荷geogson一个实现所有必需适配器的库。您只需要添加依赖项:

<dependency>
   <groupId>com.github.filosganga</groupId>
   <artifactId>geogson-core</artifactId>
   <version>1.2.21</version>
</dependency>

在下面,您可以找到如何使用它的简单示例:

import com.github.filosganga.geogson.gson.GeometryAdapterFactory;
import com.github.filosganga.geogson.model.FeatureCollection;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class GsonApp {

    public static void main(String[] args) throws Exception {
        URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake");
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

        Gson gson = new GsonBuilder()
                .registerTypeAdapterFactory(new GeometryAdapterFactory())
                .create();

        FeatureCollection collection = gson.fromJson(in, FeatureCollection.class);
        collection.features().forEach(f -> {
            System.out.println(f.properties());
        });
    }
}

应用上方的照片:

null, cdi=null, url="https://earthquake.usgs.gov/earthquakes/eventpage/us1000jimv", ids=",us1000jimv,", time=1552230050190, detail="https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us1000jimv&format=geojson", updated=1553025676040, status="reviewed"}
{dmin=0.01354, code="73150236", sources=",nc,", tz=-480, mmi=null, type="earthquake", title="M 0.3 - 8km WNW of Cobb, CA", magType="md", nst=10, sig=2, tsunami=0, mag=0.32, alert=null, gap=159, rms=0.02, place="8km WNW of Cobb, CA", net="nc", types=",geoserve,nearby-cities,origin,phase-data,scitech-link,", felt=null, cdi=null, url="https://earthquake.usgs.gov/earthquakes/eventpage/nc73150236", ids=",nc73150236,", time=1552229803430, detail="https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=nc73150236&format=geojson", updated=1552436044263, status="reviewed"}
{dmin=0.0134, code="73150231", sources=",nc,", tz=-480, mmi=null, type="earthquake", title="M 0.6 - 8km WNW of Cobb, CA", magType="md", nst=9, sig=5, tsunami=0, mag=0.57, alert=null, gap=152, rms=0.01, place="8km WNW of Cobb, CA", net="nc", types=",geoserve,nearby-cities,origin,phase-data,scitech-link,", felt=null, cdi=null, url="https://earthquake.usgs.gov/earthquakes/eventpage/nc73150231", ids=",nc73150231,", time=1552229731210, detail="https://earthquake.usgs.gov/fdsnws/event/1/query?eventid=nc73150231&format=geojson", updated=1552235523281, status="automatic"}
....

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将嵌套列表字符串转换为整数,然后在python 3中对其进行排序?

如何将 Json 列表转换为键值对象

如何将类对象列表转换为其属性列表

如何将特定的 JSONArray 转换为 JAVA 对象?

如何将json转换为对象?

如何将json对象列表转换为javascript对象列表?

如何将字节从Kafka转换为其原始对象?

如何将 MM::SS 字符串转换为时间格式然后对其进行操作

如何将JSON数组或JSON对象转换为对象列表?

如何将JSONObjects转换为JSONArray?

如何将JSONArray转换为JSONObject?

如何将JSONArray转换为ListView?

如何将JSONArray转换为arraylist

如何将JSONObject转换为JSONArray

如何将 JSONObject 转换为 JSONArray?

我有一个对象的.log文件,如何将它们转换为JSON对象以在Javascript中进行迭代?

如何将JSON对象转换为JavaScript对象

如何将C#对象转换为JSON对象

如何将 JSON 对象转换为 Python 对象?

如何将json对象列表转换为单个pyspark数据帧?

如何将Json文件(包含数组)转换为(类对象的)列表C#Unity

如何将下面的json字符串转换为C#中的对象列表

如何将一组 JSON 对象转换为 C# 列表

如何将 JSON 数组对象转换为列表数组 | 斯威夫特 5

如何将日期列表转换为 POSIXlt 对象?

如何将生成器对象转换为列表?

如何将 Class 对象列表转换为数组?

如何将 Tkinter 线对象转换为点列表

如何将javascript数组转换为特定的对象列表