如何从String解析JSON

时间:2014-04-11 14:47:14

标签: java json httprequest

我正在使用Google place API,我将其与java连接以通过JSON检索数据 我有一个存储所有JSON数据的String,我不知道接下来该做什么 我试过了

public static void sendRequest() throws ParseException, JSONException
{
    try {
        String Service = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=24.698884,46.717719&radius=50000&language=ar&types=food&sensor=true&key";
        URL url = new URL(Service);
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;
        StringBuffer response = new StringBuffer();
        line = reader.readLine(); // this String which stores the data

        while ((line = reader.readLine()) != null) {


            JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject =(JSONObject) jsonParser.parse(line);

        String name = (String) jsonObject.get("name");   
            String vicinity = (String) jsonObject.get("vicinity"); 
            String icon = (String) jsonObject.get("icon");    
            long rating=  (long)jsonObject.get("rating");

        }
        reader.close();

    } catch (MalformedURLException e) {
        e.printStackTrace() ;
    } catch (IOException e) {
       e.printStackTrace() ;
    }
}


Exception in thread "main" Unexpected token COLON(:) at position 23.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at geo.sendRequest(geo.java:51)
at geo.main(geo.java:89)

JSON是

https://developers.google.com/places/documentation/search#PlaceSearchResponses

0 个答案:

没有答案