gpt4 book ai didi

android - 解析 json 后返回 null 值,即使它不是 null

转载 作者:行者123 更新时间:2023-11-29 17:17:05 25 4
gpt4 key购买 nike

{
"page": 1,
"results": [
{
"poster_path": "/lIv1QinFqz4dlp5U4lQ6HaiskOZ.jpg",
"adult": false,
"overview": "Under the direction of a ruthless instructor, a talented young drummer begins to pursue perfection at any cost, even his humanity.",
"release_date": "2014-10-10",
"genre_ids": [
18,
10402
],
"id": 244786,
"original_title": "Whiplash",
"original_language": "en",
"title": "Whiplash",
"backdrop_path": "/6bbZ6XyvgfjhQwbplnUh1LSj1ky.jpg",
"popularity": 7.361171,
"vote_count": 1949,
"video": false,
"vote_average": 8.32
}
]
}

这是我的 JSON 响应,但当我解析它时,id 的值始终为 null下面是我的网络和 JSON 解析代码请帮助..

public class movietask extends AsyncTask<String, Void, List<movieobject>> {
private String TAG;
private static final String Base_string = "http://api.themoviedb.org/3/movie/";
private static final String api_key = "388e5684ea2f3bb8de279874cb6990a5";
private static final String MOVIE_ID="id";
private static final String MOVIE_OVERVIEW="overview";
private static final String MOVIE_TITLE="original_title";
private static final String MOVIE_VOTE="vote_average";
private static final String MOVIE_POSTERPATH="poster_path";
private static final String MOVIE_RELEASE_DATE="release_date";
private static final String MOVIE_BACKDROP_PATH="backdrop_path";

// Parse json data to get arraylist of movieobjects

private List<movieobject> getparseddata(String forecastjsonstr)
throws JSONException {
final String START_OF_JSON = "results";
JSONObject startlist = new JSONObject(forecastjsonstr);
JSONArray startarray = startlist.getJSONArray(START_OF_JSON);
int noofobjects = startarray.length();
List<movieobject> objectarray = new ArrayList<>();
for (int i = 0; i < noofobjects; i++) {
JSONObject object = startarray.getJSONObject(i);
movieobject finalobjectarray = new movieobject();
finalobjectarray.setMovieId(object.getString(MOVIE_ID));
finalobjectarray.setOverview(object.getString(MOVIE_OVERVIEW));
finalobjectarray.setTitle(object.getString(MOVIE_TITLE));
finalobjectarray.setMovieRating(object.getString(MOVIE_VOTE));
finalobjectarray.setPosterPath(object.getString(MOVIE_POSTERPATH));
finalobjectarray.setReleaseDate(object.getString(MOVIE_RELEASE_DATE));
finalobjectarray.setBackdroppath(object.getString(MOVIE_BACKDROP_PATH)+"/10");
objectarray.add(finalobjectarray);
}

return objectarray;

}



protected List<movieobject> doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
List<movieobject> finalobjectarray = new ArrayList<>();

// Will contain the raw JSON response as a string.
String forecastJsonStr = null;

try {


URL url = new URL(Base_string + params[0] + "?api_key="+api_key);


urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.

}
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}

if (buffer.length() == 0) {
return null;

}
forecastJsonStr = buffer.toString();
Log.i("hey", "Hello" + forecastJsonStr);
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}

try {
finalobjectarray = getparseddata(forecastJsonStr);
} catch (JSONException e) {
Log.e("Json exception", e.getMessage(), e);
e.printStackTrace();
}

return finalobjectarray; //final movieobject array
}


//onPostExecute method of asynctask

protected void onPostExecute(List<movieobject> results) {
}
}

最佳答案

ID 为 null 因为“id”不是字符串。

让我们检查您的 JSON。

{  
"page":1,
"results":[
{
"poster_path":"/lIv1QinFqz4dlp5U4lQ6HaiskOZ.jpg",
"adult":false,
"overview":"Under the direction of a ruthless instructor, a talented young drummer begins to pursue perfection at any cost, even his humanity.",
"release_date":"2014-10-10",
"genre_ids":[
18,
10402
],
"id":244786, <------- Without "" = Integer
"original_title":"Whiplash", <-------- With "" = String
"original_language":"en",
"title":"Whiplash",
"backdrop_path":"/6bbZ6XyvgfjhQwbplnUh1LSj1ky.jpg",
"popularity":7.361171,
"vote_count":1949,
"video":false,
"vote_average":8.32
}
]
}

因此,要获取值,您将使用 getInt()

finalobjectarray.setMovieId(object.getInt(MOVIE_ID).toString());

关于android - 解析 json 后返回 null 值,即使它不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38404026/

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