String的Json数据怎样取值

1.数据类型String

renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":115.95845799999992,"lat":35.69611689682454},"formatted_address":"山东省菏泽市郓城县","business":"","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"山东省","city":"菏泽市","city_level":2,"district":"郓城县","town":"","town_code":"","adcode":"371725","street":"","street_number":"","direction":"","distance":""},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":353}})

2.首先使用String的substring截取{}中的所有数据(通过经纬度获取位置信息)

String substring = baiduApi.substring(baiduApi.indexOf("(")+1, baiduApi.indexOf(")"));
System.out.println(substring);

3.两种解析获取自己想要的字段

    //第一种方法
        
        JSONObject jsonObject = JSONObject.parseObject(substring);
        String Address = jsonObject.getString("result");
        JSONObject object = JSONObject.parseObject(Address);
        String formattedAddress = object.getString("formatted_address");
        System.out.println("formattedAddressformattedAddress1:"+formattedAddress);

        System.out.println("================");
        //第二种方法
        JsonParser jp = new JsonParser();
//      
        shaded.com.google.gson.JsonObject jo = jp.parse(substring).getAsJsonObject();
        //获取address对应的值
        String address2 = jo.get("result").getAsJsonObject().get("formatted_address").getAsString();
        System.out.println("address2:" + address2);

你可能感兴趣的:(String的Json数据怎样取值)