/*
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package edu.pdx.its.portal.routelandia;

import com.google.android.gms.maps.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loc on 1/17/15.
 */
public class JSONParser {
    /**
     * Parse the json array which download from the web
     * @param jsonArray
     * @return the list of highways which contain the latlng
     * so the Mapsactivity can draw freeway line on the map
     */
    public List<Highway> parse(JSONArray jsonArray){
        List<Highway> highwayList =  new ArrayList<>();
        try{
            for (int i = 0; i <jsonArray.length() ; i++) {
                //Create JSON Object for each array index
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);

                //Get highwayid from the object
                int highwayid = jsonObject.getInt("highwayid");

                //Get highway name in the object
                String highwayname = jsonObject.getString("highwayname");

                //add the highway to the list highway
                highwayList.add(i, new Highway(highwayname, highwayid));

                JSONObject fullGeoJson = (JSONObject) jsonObject.get("fullGeoJson");

                //Create json array from coordinates
                JSONArray coordinates = (JSONArray) fullGeoJson.get("coordinates");

                //for each json array coordinates, create latlng and
                //add it to the list latlng of its highway
                for (int j = 0; j <coordinates.length() ; j++) {
                    double latitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(1).toString());
                    double longtitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(0).toString());
                    highwayList.get(i).addLatLng(new LatLng(latitude, longtitude));
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return highwayList;
    }
}

@@ -2607,11 +2607,2547 @@
 t;%0A    %7D
+%0A%0A    public List%3CHighway%3E parseListOfHighWay(JSONArray jsonArray)%7B%0A        List%3CHighway%3E highwayList =  new ArrayList%3C%3E();%0A        %0A        try%7B%0A            for (int i = 0; i %3CjsonArray.length() ; i++) %7B%0A                //Create JSON Object for each array index%0A                JSONObject jsonObject = (JSONObject) jsonArray.get(i);%0A%0A                //Get highwayid from the object%0A                int highwayid = jsonObject.getInt(%22highwayid%22);%0A%0A                //Get highway name in the object%0A                String highwayname = jsonObject.getString(%22highwayname%22);%0A%0A                //add the highway to the list highway%0A                highwayList.add(i, new Highway(highwayname, highwayid));%0A            %7D%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return highwayList;%0A    %7D%0A%0A    public List%3CStation%3E parseStationList(JSONArray jsonArray)%7B%0A        List%3CStation%3E stationList =  new ArrayList%3C%3E();%0A        try%7B%0A            for (int i = 0; i %3CjsonArray.length() ; i++) %7B%0A                //Create JSON Object for each array index%0A                JSONObject jsonObject = (JSONObject) jsonArray.get(i);%0A%0A                //Get stationid from the object%0A                int stationid = jsonObject.getInt(%22stationid%22);%0A%0A                //Get linked list position in the object%0A                int linkedListPosition = jsonObject.getInt(%22linked_list_position%22);%0A%0A                //add the highway to the list highway%0A                stationList.add(linkedListPosition, new Station(stationid, linkedListPosition));%0A%0A                if (!jsonObject.isNull(%22geojson_raw%22)) %7B%0A                    JSONObject geojsonRaw = jsonObject.getJSONObject(%22geojson_raw%22);%0A%0A                    //Create json array from coordinates%0A                    JSONArray coordinates = (JSONArray) geojsonRaw.get(%22coordinates%22);%0A%0A                    //for each json array coordinates, create latlng and%0A                    //add it to the list latlng of its station%0A                    for (int j = 0; j %3C coordinates.length(); j++) %7B%0A                        double latitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(1).toString());%0A                        double longtitude = Double.parseDouble(((JSONArray) coordinates.get(j)).get(0).toString());%0A                        stationList.get(i).addLatLng(new LatLng(latitude, longtitude));%0A                    %7D%0A                %7D%0A            %7D%0A%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return stationList;%0A    %7D
 %0A%7D%0A

add parse stastion list and parse highway list method