我想获取 production_companies 数组中元素的索引 如果第一家公司的“logo_path”和最后一家公司的“logo_path”为空,可能还有其他公司的“logo_path”不为空,我想得到他们的索引, 我怎样才能做到这一点?
这是一个适用于Android的电影索引应用程序 production_companies 数组中有一个公司列表 默认情况下,列表中最后一家公司的标志显示在应用程序中,但条件是它不应为空 但如果最后一个为 null ,则在应用程序中显示列表中的第一个非 null 公司
但是如果 first 和 last 都是 null ,那么 first 和 last 之间可能还有其他的不是,我想获取他们的索引并将他们的标志显示在我的应用程序中
production_companies 是数组[对象] logo_path 为 string 或 null ,字符串示例 "/q9s9KGhSsFEnpTmLLwprytB3T3d.png"
这里是来自 JSON 的示例,这里的第一个和最后一个为空
{"id":68112,"logo_path":"null","name":"Studio 100 Media","origin_country":"DE"},
{"id":104638,"logo_path":null,"name":"Studio B Animation","origin_country":""},
{"id":20187,"logo_path":"/bYdzRm9r74bIWlSRKIWZeZyDBFD.png","name":"Flying Bark Productions","origin_country":"AU"},
{"id":45198,"logo_path":"/q9s9KGhSsFEnpTmLLwprytB3T3d.png","name":"Deutscher Filmförderfonds","origin_country":"DE"},
{"id":268,"logo_path":"null","name":"FilmFernsehFonds Bayern","origin_country":"DE"},
{"id":2026,"logo_path":"null","name":"FFA","origin_country":"DE"}],
// Set Studio (production_companies)
// get the array
try {
// 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);
JSONObject firstCompany = production_companies.getJSONObject(0);
// is the "logo_path" property of lastCompany null?
if (lastCompany.getString("logo_path") == "null")
{
// then get first company ... i.e. offset zero
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) +
MizLib.getStringFromJSONObject (firstCompany, "logo_path", "")); // set the the first production company
}
// is the "logo_path" property of the firstCompany and lastCompany are null the get the index of the one which is not null
if (lastCompany.getString("logo_path") == "null" && firstCompany.getString("logo_path") == "null")
{
// Missing code here
}
else
{
// else logo_path is not null, so use lastCompany
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) +
MizLib.getStringFromJSONObject (lastCompany, "logo_path", "")); // set the last production company
}
}
} catch (JSONException e) {}
production_companies array[object]
name string
id integer
logo_path string or null
origin_country string
答案 0 :(得分:1)
使您的代码出现问题的一件事是,您将负责解析 JSON 的代码与业务逻辑混合在一起。如果将它们分开,解决方案会简单得多。
我的建议是分两步完成。
答案 1 :(得分:1)
如果您可以添加库,请添加:
implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.5.0'
然后您可以执行以下操作:
import java.util.List;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
...
Configuration cf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
DocumentContext ctx = JsonPath.using(cf).parse(moviesJson);
String firstLogo = ctx.read("$.[0].logo_path");
String lastLogo = ctx.read("$.[-1].logo_path");
// handle "null" vs null
if( firstLogo.equals("null"))
firstLogo = null;
if( lastLogo.equals("null"))
lastLogo = null;
// use last logo if it's set
if( lastLogo != null )
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) + lastLogo);
else if( firstLogo != null ) // last logo isn't set, see if we can use first
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) + firstLogo);
else { // both are null - find the first non-null one
List<String> someOtherLogo = ctx.read("$.[?(@.logo_path!='null' && @.logo_path!=null)].logo_path");
// there may be more than one - get the first one
movie.setStudioUrl(baseUrl + MizLib.getStudioUrlSize (mContext) + someOtherLogo.get(0));
}