【发布时间】:2020-06-13 18:53:48
【问题描述】:
我正在使用 Marvel API 尝试根据用户在搜索框中输入的内容来获取英雄的名字。我很确定搜索功能有效,因为它会搜索常规数组,但是我试图让它通过 JSONArray 搜索。以下是我目前所拥有的。
try {
//This is the input stream section. No idea why these need to be here but they are
//in = input
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//bin = buffered input.... i think.
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
//temp string to hold each line that's read from the reader
String inputLine;
//this keeps adding the lines to the string builder
while ((inputLine = bin.readLine()) != null) {
sb.append(inputLine);
}
//putting the string builder into a json object
JSONObject jsonObject = new JSONObject(sb.toString());
//Checking if the jsonObject has a response inside of it.
//If there is one however it is false then no results are found.
if (jsonObject.has("Response") && jsonObject.getString("Response").equals("False")){
error = true;
} else{
JSONArray results = jsonObject.getJSONArray("name");
}
这是我很确定是问题的部分,因为我认为它不会出现在其中包含名称的 JSON 部分。下面有一个 JSON 示例,当用户搜索“船长”这个词时,结果是这样的
object {7}
code : 200
status : Ok
copyright : © 2020 MARVEL
attributionText : Data provided by Marvel. © 2020 MARVEL
attributionHTML : <a href=\"http://marvel.com\">Data provided by Marvel. © 2020 MARVEL</a>
etag : 75d3eb0f8a6fd4ce06372a8e382af0fe85ea966c
data {5}
offset : 0
limit : 10
total : 19
count : 10
results [10]
0 {11}
id : 1009220
name : Captain America
description : Vowing to serve his country any way he could, young Steve Rogers
took the super soldier serum to become America's one-man army. Fighting for the
red, white and blue for over 60 years, Captain America is the living, breathing
symbol of freedom and liberty.
modified : 2020-04-04T19:01:59-0400
thumbnail {2}
resourceURI : http://gateway.marvel.com/v1/public/characters/1009220
comics {4}
series {4}
stories {4}
events {4}
urls [3]
1 {11}
2 {11}
3 {11}
4 {11}
5 {11}
6 {11}
7 {11}
8 {11}
9 {11}
我需要知道为什么它不会只将 JSON 的名称部分添加到 JSONArray。当前发生的所有情况是,当用户搜索某些内容时,以这种方式尝试时什么也没有出现。任何帮助表示赞赏。
【问题讨论】: