[英]How to get the index of this element in an Array?
I want to get the indexes of the elements in the array of production_companies if the "logo_path" of first company and the last one are null, there may be other companies for which "logo_path" is not null, I want to get their index, how can I achieve this please?如果第一个公司和最后一个公司的“logo_path”是 null,我想获取 production_companies 数组中元素的索引,可能还有其他公司的“logo_path”不是 null,我想获取他们的索引,请问我怎样才能做到这一点?
it is a movie indexing application for android the array of production_companies has a list of companies in it by default the logo of the last company on the list is displayed in the app but the condition is it should not be null but if the last one is null, then get first company on the list which is not null is displayed in the app它是 android 的电影索引应用程序 production_companies 数组默认包含公司列表 列表中最后一个公司的徽标显示在应用程序中,但条件是它不应该是 null 但如果最后一个是null,然后在应用程序中显示不是 null 的列表中的第一个公司
but if the first and last both are null, there may be others between first and last which are not, I want to get their index and get their logo displayed in my app但是如果第一个和最后一个都是 null,那么第一个和最后一个之间可能还有其他不是,我想获取他们的索引并让他们的徽标显示在我的应用程序中
production_companies is the array[object] logo_path is either string or null, the string example "/q9s9KGhSsFEnpTmLLwprytB3T3d.png" production_companies 是数组[object] logo_path 是字符串或 null,字符串示例“/q9s9KGhSsFEnpTmLLwprytB3T3d.png”
here is a sample from JSON here the first and last ones are null这是来自 JSON 的样本这里第一个和最后一个是 null
{"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
One thing that makes your code problematic is that you've mixed together code responsible for parsing JSON with business logic.使您的代码有问题的一件事是您将负责解析 JSON 的代码与业务逻辑混合在一起。 If you separate those, the solution will be much simpler.
如果你把它们分开,解决方案会简单得多。
What I would suggest then is to do it in two steps.那么我建议分两步进行。
If you're able to add a library, add:如果您能够添加库,请添加:
implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.5.0'
and you can then do something like:然后您可以执行以下操作:
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.