I have following JSONArray object from a jsonobject key "elements"

[ {
    "id":"as-a-user,-i-should-be-able-to-navigate",
      "elements":[  

    {
        "id": "navigate-down",    
        "type":"scenario"
    }
    ,
    {
        "id": "navigate-up",
        "type":"scenario"
    }
    ,
    {
        "id": ";eating",
        "examples":[ {
            "id": "as-a-user,-i-should-be-able-to-navigate;eating;", "description":"", "name":"", "keyword":"Examples", "line":23, "rows":[ {
                "id": "eating;;1", "cells": ["start", "eat", "left"], "line": 24
            }
            ,
            {
                "id": "eating;;2", "cells": ["12", "5", "7"], "line": 25
            }
            ,
            {
                "id": "eating;;3", "cells": ["20", "5", "15"], "line": 26
            }
            ]
        }
        ],
        "type":"scenario_outline"
    }
    ]
}

]

Here is the code i used to find the "rows" key details in the above JSON array.Am getting the row key values using the below code.My code using lots of JSONObjects and JSONArrays to get data.I have to do some code optimizations .How can i simplyfy my code.any better solutions?

code

JSONObject jsonObject = convertFileToJSON();
            JSONArray elementsArray= (JSONArray) jsonObject.get("elements");        
            for(int i = 0; i < elementsArray.size(); i++)
            {
                  JSONObject objects = (JSONObject) elementsArray.get(i);                
                  if(objects.get("keyword").equals("scenario_outline")){
                      JSONArray elementsArray1= (JSONArray) objects.get("examples"); 
                      JSONObject objects1 = (JSONObject) elementsArray1.get(0);                 
                      JSONArray rowArray= (JSONArray) objects1.get("rows");
                      System.out.println(rowArray.size());

                  }

            }
share|improve this question
2  
"I have to do some code optimizations ." why? what problems do you have that you need to do optimizations? – Timothy Truckle Feb 18 at 11:02

Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. (Ref: tutorials point)

Your code is perfect, not to say if that is exactly the correct form you required but in the context of memory allocations.

Honestly, your code is neat and clean. You probably do not require any changes for optimisation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.