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.
asked Mar 29, 2012 at 11:39
user1092042
1,2955 gold badges25 silver badges44 bronze badges
-
Simply reiterate through the JSON object and pick first item. If item has underscore, pick the next using a flag.Straseus– Straseus2012年03月29日 11:41:44 +00:00Commented 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.Chetter Hummin– Chetter Hummin2012年03月29日 11:43:14 +00:00Commented Mar 29, 2012 at 11:43
-
Could you please post some code as im a little confused right now.user1092042– user10920422012年03月29日 11:43:49 +00:00Commented Mar 29, 2012 at 11:43
1 Answer 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;
}
answered Mar 29, 2012 at 12:27
anvarik
6,5075 gold badges42 silver badges53 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java