如何获取数组中该元素的索引?

时间:2021-05-28 19:49:39

标签: java android arrays

我想获取 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

2 个答案:

答案 0 :(得分:1)

使您的代码出现问题的一件事是,您将负责解析 JSON 的代码与业务逻辑混合在一起。如果将它们分开,解决方案会简单得多。

我的建议是分两步完成。

  1. 将 json 解析为 POJO 类。查看您的数据,它可能只是一个公司列表(或数组)。公司是简单的java类。您可以使用 GSON 库等自动完成此步骤。
  2. 一旦您拥有带有对象的 java 数组中的所有数据,请对其进行迭代以找到您感兴趣的数据(在您的情况下具有非空徽标路径)。

答案 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));
}
相关问题