1

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"
}

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.

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");

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

3
  • 2
    Why can't you just iterate over the JSONArray, counting the index, and stop when you find a match? Commented May 30, 2014 at 7:11
  • Iterate over your VALUES array in a for loop until you find your match, and use that index to grab the right display value from DISPLAY_VALUES. Commented May 30, 2014 at 7:12
  • @bstar55 yeah I guess I can just do that. I thought there could be some method of JSONArray that returns the index given the element, without us going through a loop. Commented May 30, 2014 at 7:19

1 Answer 1

4

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);

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

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to solve a similar problem can a take a look at this question stackoverflow.com/questions/34151997/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.