gpt4 book ai didi

java - 从 JSONArray 获取字符串到 String[] 或 List 以输入到 ArrayList

转载 作者:行者123 更新时间:2023-12-02 12:32:26 25 4
gpt4 key购买 nike

我有这个 JSON 代码,并已成功为其他键创建了对象,但“authors”键给我带来了问题。我已将数据检索到单个字符串中,但它包含不需要的字符,因此在我的适配器类中对其进行了格式化,但我觉得这很草率。

这是 JSON 的 fragment

    {  
"kind":"books#volumes",
"totalItems":2816,
"items":[
{
"kind":"books#volume",
"id":"6tLAyQLSzG0C",
"etag":"N70szFickpY",
"selfLink":"https://www.googleapis.com/books/v1/volumes/6tLAyQLSzG0C",
"volumeInfo":{
"title":"Android for Work",
"subtitle":"Productivity for Professionals",
"authors":[
"Marziah Karch"
],
"publisher":"Apress",
"publishedDate":"2010-09-01",

这是我从 JSON 结果中检索数据的方法 -

public static ArrayList<Books> extractBooks(String jsonString) {
ArrayList<Books> books = new ArrayList<>();
String title;
String[] authors;
String published;
String description;
String url;
Bitmap bitmap;
JSONObject imageLinks = null;

try {
JSONObject baseJsonObject = new JSONObject(jsonString);
JSONArray jsonArray = baseJsonObject.getJSONArray("items");


for (int i = 0; i < jsonArray.length(); i ++) {
JSONObject currentBook = jsonArray.getJSONObject(i);
JSONObject items = currentBook.getJSONObject("volumeInfo");
JSONArray authorArray = items.getJSONArray("authors");
authors = new String[authorArray.length()];
for (int a = 0; a < jsonArray.length(); a++) {
authors[a]= jsonArray.getString("authors");
Log.i(LOG_TAG, "String[] authors: " + authors);
}
//TODO: Fixed? Keep testing.
if (items.has("imageLinks")) {
imageLinks = items.getJSONObject("imageLinks");
String image = imageLinks.getString("smallThumbnail");
bitmap = extractImages(image);
} else {
bitmap = extractImages("http://books.google.com/books/content?id=M_bMehaHiG0C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api");
}

Boolean hasCover = items.has("imageLinks");
Log.i(LOG_TAG, "has imageLinks - " + hasCover.toString());



if (items.has("title")){
title = items.getString("title");
} else {
title = "No title available";
}
//TODO: Fix authors value to "list" or "array" rather than patching with String.replace in BooksAdapter
// if (author.has("authors")){
// author = items.toString("authors");
// } else {
// author = ("No author available");
// }
if (items.has("publishedDate")) {
published = items.getString("publishedDate");
} else {
published = null;
}
if (items.has("description")){
description = items.getString("description");
} else {
description = "No description available";
}
if (items.has("infoLink")) {
url = items.getString("infoLink");
} else {
url = "none";
}
//TODO:Add key for ebook:True||False so can add icon over cover for Google Play Books




Books book = new Books(title, bitmap, published, description, url, authors);
books.add(book);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing JSON book results", e);
}

return books;
}

如您所见,这项工作正在进行中!我正在尝试创建一个 for 循环来循环遍历“authors”键并将结果保存在 String[] 中。它不起作用,我收到错误“对作者没有值(value)”。我可以看到,一旦我转到该键,我就没有任何引用值(value),但我不知道如何继续。

最佳答案

authors":[  // your json array
"Marziah Karch"
],

so authors 包含字符串值,因此您应该使用索引 a 检索它们

你需要使用

authors[a] = authorArray.getString(a);
// i recommend to use optString as authorArray.optString(a);

关于java - 从 JSONArray 获取字符串到 String[] 或 List<String> 以输入到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45242128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com