gpt4 book ai didi

java - 在 Android 上解析时,Json 值返回空(如果不是)

转载 作者:行者123 更新时间:2023-11-30 08:42:45 29 4
gpt4 key购买 nike

我正在尝试在 Android 上读取此 json 字符串,但字段“thumbnail”总是返回空,即使它实际上从未为空,它应该返回一个 url 或“self”。

{
"kind": "t3",
"data": {
"domain": "twitter.com",
"banned_by": null,
"media_embed": {},
"subreddit": "nba",
"id": "3y6vzw",
"author": "Higgnkfe",
"media": null,
"score": 883,
"approved_by": null,
"over_18": false,
"hidden": false,
"num_comments": 811,
"thumbnail": "http://b.thumbs.redditmedia.com/rfBhblr-DJIfsCEwQ9jq-wy_mHcmmedx3_Xy60STumc.jpg",
"subreddit_id": "t5_2qo4s",
"stickied": false,
"from": null,
"is_self": false,
"from_id": null,
"permalink": "/r/nba/comments/3y6vzw/first_all_star_results_are_in_kobe_steph_lead_the/",
"created": 1451088664.0,
"url": "https://twitter.com/nbaallstar/status/680417825463365632",
"title": "First All Star Results are in; Kobe, Steph lead the vote",
"created_utc": 1451059864.0,
"ups": 883
}

这是我用来阅读的一段代码:

        String title, author, subreddit, id, thumbnail = "test", url;
int score, numOfComments;
double created;
boolean isSelf;

JSONArray arr = new JSONObject(raw).getJSONObject("data").getJSONArray("children");

for (int i = 0; i < arr.length(); i++) {
JSONObject data = arr.getJSONObject(i).getJSONObject("data");

Log.d("LOADER", "JSON: " + data.toString());

title = data.getString("title");
author = data.getString("author");
subreddit = data.getString("subreddit");
id = data.getString("id");
thumbnail = data.getString("thumbnail");
score = data.getInt("score");
Log.d("LOADER", " thumb: " + thumbnail); // log shows "thumb:" only
url = data.getString("url");
created = data.getDouble("created");
isSelf = data.getBoolean("is_self");
numOfComments = data.getInt("num_comments");

Post post = new Post(subreddit, title, author, url, id, score, numOfComments,
thumbnail, created, isSelf);

posts.add(post);
}

我读取任何其他字段都没有问题,即使是也包含 url 的“url”字段,所以不可能是...

这是日志为 data.toString() 显示的内容:

JSON: {"domain":"self.nba","banned_by":null,.....(etc etc etc).......,"id":"3y46dt","num_comments":787,"thumbnail":"","subreddit_id":"t5_2qo4s","is_self":true,"from_id":null,"url":"https:\/\/www.reddit.com\/r\/nba\/comments\/3y46dt\/trash_talk_thread_no_games_today_because_of\/","author_flair_text":"[TOR] Luis Scola"....etc  

如您所见,“thumbnail”的值为“”,但“url”确实有一个 url。

这是怎么回事?

这是我从中获取 json 字符串的 url,以防它有用 http://www.reddit.com/r/nba/.json

最佳答案

enter image description here我自己检查了一下,你的代码没有问题。事实上,您获得的 json 包含 缩略图 值。我已转到您提供的链接开始的 3 个数组包含空值,用于缩略图。所以你的代码是正确的,它与缩略图的值无关,无论它是 url 还是其他值。

关于java - 在 Android 上解析时,Json 值返回空(如果不是),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466634/

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