如何解析具有相同结构但名称不同的json数组

时间:2013-10-16 08:08:54

标签: java android json gson

我想解析一个json文件,但它是这样的:

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: ""
},

但是大约有三千个像这样的对象。我一直在使用Gson来解析我的json对象,但是我如何解析这种文件呢?如何检索名称“CDG”或“ORY”?

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

  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"
                + "}}";

使用gson,您可以按如下方式检索密钥名称:

        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));
        }

并使用java-json,您可以执行以下操作:

    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"));
        }

从url获取json:

   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", "");

答案 1 :(得分:0)

ArrayList<String> keylist = new ArrayList<String>();

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

所以从这里你可以获得没有任何tnsn的地图的所有关键字,只需将它添加到arraylist中所以只需将密钥作为arraylist索引值传递并获取map的值,所以你不用担心json密钥,无论它是什么是否会存储在arraylist索引中。 :)