gpt4 book ai didi

java - 如何从 JSON 中提取图像的 url(LastFM API)

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

我是 JSON 使用的初学者。

所以我尝试从 JSON 回复中提取图像的 url。

这是允许我获取数组的代码:

            // Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getActivity());
//String url ="http://www.google.com";
String url = "http://ws.audioscrobbler.com/2.0/?method=album.search&album="+albumName+"&api_key=c51f8eb36bad&format=json";


// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
//mTextView.setText("Response is: "+ response.substring(0,500));
Log.i("RESPONSE","Response is: "+ response);

JSONObject jsono = new JSONObject();
try {
jsono = new JSONObject(response);
//String url = jsono.getString("results");


//Log.i("RESPONSE",url);
} catch (JSONException e) {
e.printStackTrace();
Log.d ("RESPONSE",e.getMessage());
}
JSONArray jsonArray = new JSONArray();
try {
jsonArray = jsono.getJSONObject("results").getJSONObject("albummatches").getJSONArray("album");
} catch (JSONException e) {
e.printStackTrace();
Log.d ("RESPONSE",e.getMessage());
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = new JSONObject();
try {
object = jsonArray.getJSONObject(i);
Log.i("RESPONSE",object.getString("image"));

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

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("RESPONSE","That didn't work!");
}
});
queue.add(stringRequest);

这是 JSON 答案中这部分的结构:

    {

"album": [
{
"name": "DD Y Ponle Play",
"artist": "Jumbo",
"id": "2528039",
"url": "http://www.last.fm/music/Jumbo/DD+Y+Ponle+Play",
"image": [
{
"#text": "http://images.amazon.com/images/P/B00005LN6S.01._SCMZZZZZZZ_.jpg",
"size": "small"
},
{
"#text": "http://images.amazon.com/images/P/B00005LN6S.01._SCMZZZZZZZ_.jpg",
"size": "medium"
},
{
"#text": "http://images.amazon.com/images/P/B00005LN6S.01._SCMZZZZZZZ_.jpg",
"size": "large"
},
{
"#text": "http://images.amazon.com/images/P/B00005LN6S.01._SCMZZZZZZZ_.jpg",
"size": "extralarge"
}
]
}
]
}

如何获取给定尺寸的图像的 URL?

非常感谢您的建议。

最佳答案

您可以使用谷歌 GSON 来实现此目的。将其作为依赖项导入

首先创建一个相册类。

public class Albums {

private List<Album> album;

public List<Album> getAlbum() {
return album;
}

public class Album{
private String name;
private String artist;
private String id;
private String url;

public String getName() {
return name;
}

public String getArtist() {
return artist;
}

public String getId() {
return id;
}

public String getUrl() {
return url;
}

public List<Image> getImage() {
return image;
}

public class Image {
@SerializedName("#text")
private String text;
private String size;

public String getText() {
return text;
}

public String getSize() {
return size;
}
}

private List<Image> image;
}

}

现在,在您获取上述 JSON 对象的代码中尝试下面的代码

Gson gson = new Gson();

// Im assuming "response" as the above JSON object
Albums albums = gson.fromJson(response.optString("album"),Albums.class);

这会将您的 json 映射到 java 对象。(注意:您可以根据需要从 POJO 中删除不需要的对象)

您可以使用 getter 函数获取图像

关于java - 如何从 JSON 中提取图像的 url(LastFM API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44290656/

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