在JSONArray中查找元素的索引 [英] Find index of element in a JSONArray

查看:497
本文介绍了在JSONArray中查找元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器接收到如下的JSONObject:

I receive a JSONObject from server that is as follows:

{
   "opOutput":{
      "DISPLAY_VALUES":[
         "Acceptance",
         "Arrive Load Port",
         "Arrive Receipt Point",
         "Arrive Relay Port",
         "Arrive Terminal",
         "Arrived at Delivery Location",
         "Arrived at Pickup Location",
         "Arrived Intermodal Train",
         "At Customs",
         .
         .
         .
         .  


      ],
      "VALUES":[
         "ACCPT",
         "ALPT",
         "ARRP",
         "ARREL",
         "ATRM",
         "QARD",
         "ARPUL",
         "AIMTRN",
         "K",
         .
         .
         .
         .
      ]
   },
   "_returnValue":{
      "TX_TYPE":"SHIPMENT",
      "__DeltaStatus":2,
      "ORG_CODE":"GFM",
      "TX_ID":"11019082"
   },
   "_returnType":"SUCCESS"
}

现在,我需要获取s String的显示值,该值等于其中一个值.即我有字符串"ACCPT",我需要从JSONObject中获取"Acceptance".

Now I need to get the display value for s String s that is equal to one of the values. i.e I have string "ACCPT" and i need to get "Acceptance" from the JSONObject.

我用DISPLAY_VALUES和VALUES制作了两个JSONArrays

I've made two JSONArrays with DISPLAY_VALUES and VALUES with

JSONObject opoutput=shipmentcodes.getJSONObject("opOutput");
JSONArray event_values=opoutput.getJSONArray("DISPLAY_VALUES");
JSONArray event_codes=opoutput.getJSONArray("VALUES");

其中,shipmentcodes是原始的JSONObject,但是我不确定如何继续进行操作.有提示吗?

where shipmentcodes is the original JSONObject, but I'm unsure about how to proceed further. Any tips?

推荐答案

JSONArray 中的值添加到 List 中,并使用 indexOf 方法

Add the values from JSONArray to a List and use the indexOf method

JSONArray event_values = opoutput.getJSONArray("DISPLAY_VALUES");
JSONArray event_codes = opoutput.getJSONArray("VALUES");

List<String> valueList = new ArrayList<String>();
List<String> displayList = new ArrayList<String>();
for(int i=0;i<event_codes.length();i++){
        // if both event_values and event_codes are of equal length
        valueList.add(event_codes.getString(i));
        displayList.add(event_values.getString(i));
    }

int index = valueList.indexOf("ACCPT");
String valueToDisplay = displayList.get(index);

然后您可以使用 valueToDisplay 显示所需的值.

You can then use valueToDisplay for displaying the value you need.

这篇关于在JSONArray中查找元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆