json包的不同导致结果不同
引入如下json jar包
1 import org.json.JSONArray;
2 import org.json.JSONObject;
代码:
1 JSONObject jsonObject = new JSONObject(stringEntity); 2 3 JSONObject jsonCity =jsonObject.getJSONObject("data"); 4 city = jsonCity.getString("city"); 5 //获取response的body 6 System.out.println("Ip "+Ip+"aliIpInfo response body"+stringEntity+"city"+city);
结果:
Ip 110.53.253.205aliIpInfo response body{"code":0,"data":{"area":"华中","area_id":"400000","city":"长沙市","city_id":"430100","country":"中国","country_id":"CN","county":"","county_id":"","ip":"110.53.253.205","isp":"联通","isp_id":"100026","region":"湖南省","region_id":"430000"}} city 长沙市
但其他的包:
import org.json.simple.JSONObject;
测试得不到值,所以一定要选对json包。