Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

parsing json with java

I am a newbie to json parsing, I have grabbed a json string from a request and now I need to parse it with java. I'm using json-lib for this. But I'm really stuck as I'm not familiar with it. I need to extract following data

1. name (hotel name)
2. starRating
3. geoPoint

I used following java code for that but it's not giving me the result I need, please someone help me...

Thanks a lot!

java code (s is the json string I get)

JSONObject json = (JSONObject) JSONSerializer.toJSON(s);    
JSONArray jarray = json.getJSONArray("hotels");
for(int i=0 ; i < jarray.size(); i++) {
System.out.println("jarray [" + i + "] --------" + jarray.getString(i));
}

json I need to parse

[
{
    "total": 250,
    "offset": 0,
    "requestID": "-btygi09oxfov",
    "locationName": "Paris, France",
    "locationLatitude": 48.86,
    "locationLongitude": 2.34,
    "cityCode": "PARIS_J_FR",
    "hotels": [
        {
            "ypid": "YN10001x300073304",
            "id": 56263,
            "hotelRateIndicator": "2",
            "name": "Renaissance Paris Vendome Hotel",
            "brandCode": "69",
            "addressLine1": "4 Rue du Mont-Thabor",
            "city": "Paris",
            "neighborhood": "",
            "state": "IdF",
            "country": "US",
            "cachedPrice": 935,
            "geoPoint": [
                48.865361,
                2.329584
            ],
            "starRating": "5",
            "thumbnailUrl": "http://www.orbitz.com//public/hotelthumbnails/53/97/85397/85397_TBNL_1246535840051.jpg",
            "total": 250,
            "amenities": [
                "24",
                "31",
                "42",
                "52",
                "9"
            ],
            "telephoneNumbers": [
                ""
            ],
            "popularity": 837
        },
        {
            "ypid": "YN10001x300073331",
            "id": 112341,
            "hotelRateIndicator": "3",
            "name": "Renaissance Paris Arc de Triomphe Hotel",
            "brandCode": "69",
            "addressLine1": "39 Avenue de Wagram",
            "city": "Paris",
            "neighborhood": "",
            "state": "IdF",
            "country": "US",
            "cachedPrice": 633,
            "geoPoint": [
                48.877107,
                2.297451
            ],
            "starRating": "5",
            "thumbnailUrl": "http://www.orbitz.com//public/hotelthumbnails/21/72/302172/302172_TBNL_1246535872514.jpg",
            "total": 250,
            "amenities": [
                "24",
                "31",
                "42",
                "9"
            ],
            "telephoneNumbers": [
                ""
            ],
            "popularity": 796
        }           
  ]         
}           
  ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Any JSON object can be represented as a Map<String, Object>.

Use a library like jackson (shipped with spring), which can deserialize json to a Map like this:

Map<String, Object> obj = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>());

Or the slower, but google-branded, GSON, which can be used like.

Map<String, Object> obj = new Gson().fromJson(json, HashMap.class);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...