【问题标题】:De-serializing nested JSON to Java in Android Studio在 Android Studio 中将嵌套的 JSON 反序列化为 Java
【发布时间】:2020-07-19 01:24:27
【问题描述】:

我正在尝试将 this JSON 反序列化为 Mural 类的 Java 对象数组,并在我的应用程序的列表(回收器视图)中显示它们。这是 JSON 的格式化示例,其中仅包含我需要的数据。

{
"records": [{ 
    "recordid": "e6b10b2f7cadbe7caec9a0b36e9e7b570c3add50", 
    "fields": {
        "auteur_s": "Dupa", 
        "photo": {
            "id": "7cd8a2bc799826835057eaec21a28730", 
            },
        "annee": "1994",
        "coordonnees_geographiques": [50.8527886675, 
                          4.34530735016], 
        "personnage_s": "Cubitus - Dommel"
        }, 
    },
    ...
    ]
}

我使用Okhttp发出请求并且响应成功,所以我使用它来构造一个JSONObject。 我遍历该对象中的"records" 数组,因为在那里我找到了我需要的所有数据。具有字符串或数组作为值的键工作正常,我可以将它们加载到我的应用程序的 Recycler 视图中,但是当我想访问 "photo",即嵌套在另一个对象中的键:值对时,我得到一个 JSONException @987654326 @ 并且我的回收站视图返回空。

代码如下:

private void fetchMurals(){
        threadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder()
                        .url("https://bruxellesdata.opendatasoft.com/api/records/1.0/search/?dataset=comic-book-route&rows=58")
                        .get()
                        .build();
                try{ 
                    Response response = client.newCall(request).execute();
                    String json = response.body().string();

                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray jsonRecordsArray = jsonObject.getJSONArray("records");

                    int arraySize = jsonRecordsArray.length();
                    muralArrayList = new ArrayList<>();

                    for (int i = 0; i < arraySize; i++){
                        String jsonID = jsonRecordsArray.getJSONObject(i).getString("recordid");
                        JSONObject jsonMural = jsonRecordsArray.getJSONObject(i).getJSONObject("fields");
                        JSONObject jsonMuralPhoto = jsonMural.getJSONObject("photo");

                        final Mural currentMural = new Mural(
                                jsonID,
                                (jsonMural.has("auteur_s")) ? jsonMural.getString("auteur_s") : "Unknown Author",
                                (jsonMuralPhoto.has("id")) ? jsonMuralPhoto.getString("id") : "No picture available!",
                                (jsonMural.has("personnage_s")) ? jsonMural.getString("personnage_s") : "Unknown character",
                                (jsonMural.has("annee")) ? jsonMural.getString("annee") : "Unknown year of creation",
                                new LatLng(jsonMural.getJSONArray("coordonnees_geographiques").getDouble(0),
                                           jsonMural.getJSONArray("coordonnees_geographiques").getDouble(1))
                        );
                        muralArrayList.add(currentMural);

                } catch (IOException e){
                    e.printStackTrace();
            }
        });
    }

我已经尝试了很多变体,但到目前为止都没有奏效。我的循环可能有问题吗?或者我打电话给jsonMural.getJSONObject("photo")

【问题讨论】:

    标签: java android json parsing json-deserialization


    【解决方案1】:

    我现在看到我正在检查我的记录是否有“id”字段,而我应该检查它是否有“照片”。我将构造函数中的行更改为

    (jsonArtwork.has("photo")) ? jsonArtwork.getJSONObject("photo").getString("id") : "No picture available!"
    

    现在工作。

    【讨论】:

      猜你喜欢
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2020-09-29
      • 1970-01-01
      • 2022-01-20
      • 2017-04-13
      • 1970-01-01
      相关资源
      最近更新 更多