1

I want to parse a json file but it goes by something like this :

CDG: {
id: "32569",
airport_name: "Charles De Gaulle",
latitude: "49.0167",
longitude: "2.55",
timezone: "2",
dst_indicator: "E",
city: "Paris",
country: "France",
country_code: "FR",
region: "TC1",
listing_display: "true",
pseudonyms: ""
},
ORY: {
id: "33539",
airport_name: "Orly",
latitude: "48.7167",
longitude: "2.3833",
timezone: "2",
dst_indicator: "E",
city: "Paris",
country: "France",
country_code: "FR",
region: "TC1",
listing_display: "true",
pseudonyms: ""
},
LBG: {
id: "123425",
airport_name: "Le Bourget",
latitude: "48.969444",
longitude: "2.441389",
timezone: "1",
dst_indicator: "E",
city: "Paris",
country: "France",
country_code: "FR",
region: "TC1",
listing_display: "true",
pseudonyms: ""
},

But there is about three thousands object like this. I've been always using Gson to parse my json objects, but how can i parse this kind of file ? And how can i retrieve the name "CDG" or "ORY" ?

2 Answers 2

2

You can try something like this:

  String str = "{CDG: {\n"
                + "id: \"32569\",\n"
                + "airport_name: \"Charles De Gaulle\",\n"
                + "latitude: \"49.0167\",\n"
                + "longitude: \"2.55\",\n"
                + "timezone: \"2\",\n"
                + "dst_indicator: \"E\",\n"
                + "city: \"Paris\",\n"
                + "country: \"France\",\n"
                + "country_code: \"FR\",\n"
                + "region: \"TC1\",\n"
                + "listing_display: \"true\",\n"
                + "pseudonyms: \"\"\n"
                + "},\n"
                + "ORY: {\n"
                + "id: \"33539\",\n"
                + "airport_name: \"Orly\",\n"
                + "latitude: \"48.7167\",\n"
                + "longitude: \"2.3833\",\n"
                + "timezone: \"2\",\n"
                + "dst_indicator: \"E\",\n"
                + "city: \"Paris\",\n"
                + "country: \"France\",\n"
                + "country_code: \"FR\",\n"
                + "region: \"TC1\",\n"
                + "listing_display: \"true\",\n"
                + "pseudonyms: \"\"\n"
                + "},\n"
                + "LBG: {\n"
                + "id: \"123425\",\n"
                + "airport_name: \"Le Bourget\",\n"
                + "latitude: \"48.969444\",\n"
                + "longitude: \"2.441389\",\n"
                + "timezone: \"1\",\n"
                + "dst_indicator: \"E\",\n"
                + "city: \"Paris\",\n"
                + "country: \"France\",\n"
                + "country_code: \"FR\",\n"
                + "region: \"TC1\",\n"
                + "listing_display: \"true\",\n"
                + "pseudonyms: \"\"\n"
                + "}}";

Using gson, you can retrieve the key names as follows:

        Gson gson = new Gson();
        Object o = gson.fromJson(str, Object.class);
        List keys = new ArrayList();
        Collection values = null;
        if (o instanceof Map) {
            Map map = (Map) o;
            keys.addAll(map.keySet()); // collect keys at current level in hierarchy
            values = map.values();
        } else if (o instanceof Collection) {
            values = (Collection) o;
        }
        System.out.println(keys);// [CDG, ORY, LBG]
        for (int i = 0; i < keys.size(); i++) {
            System.out.println(keys.get(i));
        }

and using java-json you can do as follows:

    JSONObject jsonObject = new JSONObject(str);
        String[] names = jsonObject.getNames(jsonObject);
        for (int i = 0; i < names.length; i++) {

            System.out.println(names[i]);// all names are printed here : LBG,ORY, etc

            // then parsing the names accordingly..
            JSONObject jsonObject1 = jsonObject.getJSONObject(names[i]);
            System.out.println(jsonObject1.getString("city"));
        }

For fetching json from url:

   public static String connectionGet(String url, String parameter) throws MalformedURLException, ProtocolException, IOException {

    URL url1 = new URL(url);
    HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
    request1.setRequestMethod("GET");
    request1.connect();
    String responseBody = convertStreamToString(request1.getInputStream());
    return responseBody;
}

String str = connectionGet("http://www.cleartrip.com/common/json/airports.json", "");
14
  • He want this to be done using Gson! Oct 16, 2013 at 8:32
  • Using GSON then in that case objects have to be defined for them. one for the first, and the other for the second having the same properties, and talking about 3000 such objects :O.. you could look into gson tutorial site :). Oct 16, 2013 at 8:46
  • I'm still going to try this way.
    – Tsunaze
    Oct 16, 2013 at 8:46
  • @LalitPoptani have edited the answer , now the names can be retrieved using gson.
    – Jhanvi
    Oct 16, 2013 at 8:53
  • @Tsunaze have edited the answer, you can do it using gson also
    – Jhanvi
    Oct 16, 2013 at 8:54
0
ArrayList<String> keylist = new ArrayList<String>();

            for ( String key : your_map_name.keySet() ) {
                System.out.println( key );
                keylist.add(""+key);
            }

so from this you can get all the key of map without any tnsn and just add it into the arraylist so after just pass key as a arraylist index value and get the value of map so u not worry about the json key whatever it is it will be store in the arraylist index. :)

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