0

I have to parse the following data

{"ResultSet":{"Query":"microsec fin","Result":
[{"symbol":"MICROSE_a.NS","name": "MICROSEC FIN SERV LTD ","exch": "NSI","type": "S","exchDisp":"NSE","typeDisp":"Equity"},
   {"symbol":"MICROSEC.NS","name": "Microsec Fin Serv Ltd","exch": "NSI","type": "S","exchDisp":"NSE","typeDisp":"Equity"}]}}

The code i am using is

JSONObject json = (JSONObject) JSONSerializer.toJSON(inputLine);
symbol=json.getJSONObject("ResultSet").getJSONArray("Result").getJSONObject(0).getString("symbol"); 

which returns MICROSE_a.NS. What i want to do is if there is an undersore in symbol then i want the next symbol to be taken. That is now i want symbol to actually hold MICROSEC.NS. How do i do this.

3
  • Simply reiterate through the JSON object and pick first item. If item has underscore, pick the next using a flag. Commented Mar 29, 2012 at 11:41
  • You'll have to check the current symbol for _ and then select the next symbol using the same statement. You could probably put this in a loop. Commented Mar 29, 2012 at 11:43
  • Could you please post some code as im a little confused right now. Commented Mar 29, 2012 at 11:43

1 Answer 1

1

Or which is much simpler with your library:

JSONArray Result= json.getJSONObject("ResultSet").getJSONArray("Result");
for(int i = 0; i<Result.length(); i++){ 
    String symbol = Result.getJSONObject(i).getString("Symbol");
    if(!symbol.contains("_"))
          return symbol;
}
Sign up to request clarification or add additional context in comments.

Comments

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.