如果ArrayList中的元素不为null,如何通过使用for循环来检索该元素的值



在我的应用程序中有一个数据抓取器,它从themoviedb.orgapi检索有关电影的信息,我想检索的一个项目是production_companieslogo_path,它作为JSONArray提供,但logo_pathvalue可能是null(下面的示例(

我的目标是实现以下目标:-

例如JSONArray提供了6个元件(6个生产公司(

如果列表的最后一个元素(索引5(的logo_pathvaluenull,那么我想检索不是null的第一个元素(指数0(的值

如果列表的最后一个元素(索引5(的logo_path的值是而不是null,那么我想检索最后一个元件(索引5

如果第一个是null,最后一个也是null,那么我想检索其中一个不是nullvalue

更新

生产_公司示例

"production_companies":[
{"id":429,"logo_path":"/2Tc1P3Ac8M479naPp1kYT3izLS5.png","name":"DC Comics","origin_country":"US"},
{"id":923,"logo_path":"/5UQsZrfbfG2dYJbx8DxfoTr2Bvu.png","name":"Legendary Pictures","origin_country":"US"},
{"id":9996,"logo_path":"/3tvBqYsBhxWeHlu62SIJ1el93O7.png","name":"Syncopy","origin_country":"GB"},
{"id":118865,"logo_path":null,"name":"Isobel Griffiths","origin_country":"GB"},
{"id":9993,"logo_path":"/2Tc1P3Ac8M479naPp1kYT3izLS5.png","name":"DC Entertainment","origin_country":"US"},
{"id":174,"logo_path":"/IuAlhI9eVC9Z8UQWOIDdWRKSEJ.png","name":"Warner Bros. Pictures","origin_country":"US"}]

我的代码

try {
JSONArray production_companies = jObject.getJSONArray("production_companies");
for (int i = 0; i < production_companies.length(); i++) {
if (jObject.getString(name: "logo_path").equalsIgnoreCase(string: "null")) // when the "logo_path" is "null"
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) + MizLib.getStringFromJSONObject (production_companies.getJSONObject (i - i), "logo_path", "")); // set the the first production company
else if (!jObject.getString ("logo_path").equalsIgnoreCase ("null")) // when the "logo_path" is not "null"
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) + MizLib.getStringFromJSONObject (production_companies.getJSONObject (i+1), "logo_path", "")); // set the the first production company
}
} catch (JSONException e) {}

api提供的数据类型

production_companies array[object]
name             string
id               integer
logo_path        string or null
origin_country   string

从您的问题中还不清楚JSON的实际外观,logo_path值是一个值为"的字符串吗;空";,或者logo_path元素实际上为空。以下假设为后者。

您写道:"当列表中最后一个生产公司的logo_ path的值等于null时。。。否则";我想把最后一家制作公司">

请跟随这个代码和我添加的评论:

// get the array
JSONArray production_companies = jObject.getJSONArray("production_companies");
// ensure array has at least one entry
if (production_companies.length() > 0)
{
// get the last element of the array as JSONObject (offset is length of array - 1)
JSONObject lastCompany = production_companies.getJSONObject(production_companies.length() - 1);
// is the "logo_path" property of lastCompany null?
if (lastCompany.getString(name:"logo_path").equals("null"))
{
// then get first company ... i.e. offset zero
JSONObject firstCompany = production_companies.getJSONObject(0);
// set the first production company
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) +
MizLib.getStringFromJSONObject (firstCompany, "logo_path", "")); 
}
else
{
// else logo_path is not null, so use lastCompany
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) +
MizLib.getStringFromJSONObject (lastCompany, "logo_path", "")); 
}
}
if(jObject.getString("logo_path").equalsIgnoreCase("null"))

这一行正在检查它是否是字符串文字";空";如果要检查getString("logo_path"(是否返回null,则需要将其更改为

if(jObject.getString("logo_path") == null)

我确信您可以简单地尝试:logo_path==null。如果它不起作用,您可以始终将字符串定义为"-1〃;当它不包含任何内容时。希望我能帮上忙。

最新更新