JSON处理

 

{
    "has_more": true,
    "items": [
        {
            "mayor_description": null,
            "tel": "",
            "name": "锦秋国际大厦",
            "lon": 116.348678553941,
            "has_event": false,
            "has_surprise": false,
            "has_mayor_coupon": false,
            "link": "http://jie.pn/l/JGeulc",
            "lat": 39.9758721296206,
            "dist": "300 m",
            "guid": "EB005A8F7F00569A8B786D3A9DD4F47B",
            "categories": [
                {
                    "is_primary": 1,
                    "id": "0702",
                    "name": "写字楼/办公室",
                    "icon": "/static/img/categories/building/office.gif"
                }
            ],
            "addr": "北京海淀区知春路6号"
        },
        {
            "mayor_description": null,
            "tel": "010-82058553",
            "name": "太月园",
            "lon": 116.3466,
            "has_event": false,
            "has_surprise": false,
            "has_mayor_coupon": false,
            "link": "http://jie.pn/l/7oKRDb",
            "lat": 39.9737406451952,
            "dist": "200 m",
            "guid": "BB6675B3018AB6E0",
            "categories": [
                {
                    "is_primary": 1,
                    "id": "0704",
                    "name": "公寓/小区/里弄",
                    "icon": "/static/img/categories/building/house.gif"
                }
            ],
            "addr": "北京海淀区知春路太月园小区"
        },
        {
            "mayor_description": null,
            "tel": "",
            "name": "新岛咖啡(蓟门里店)",
            "lon": 116.34994109522,
            "has_event": false,
            "has_surprise": false,
            "has_mayor_coupon": false,
            "link": "http://jie.pn/l/EjdWC",
            "lat": 39.973350860007,
            "dist": "100 m",
            "guid": "9E29683C1B00793BA29DA480A0B1DEA0",
            "categories": [
                {
                    "is_primary": 1,
                    "id": "0102",
                    "name": "咖啡馆",
                    "icon": "/static/img/categories/food/cafe.gif"
                }
            ],
            "addr": "北京海淀区蓟门桥北"
        },
        {
            "mayor_description": null,
            "tel": null,
            "name": "湘识府",
            "lon": 116.346384,
            "has_event": false,
            "has_surprise": false,
            "has_mayor_coupon": false,
            "link": "http://jie.pn/l/tfQx0",
            "lat": 39.973765,
            "dist": "250 m",
            "guid": "68B42FFD5AD81F6343A08CD73E1C15E8",
            "categories": [
                {
                    "is_primary": 1,
                    "id": "0121",
                    "name": "湖南菜",
                    "icon": "/static/img/categories/food/chinese.gif"
                }
            ],
            "addr": "海淀区知春路"
        }
    ],
    "province": "北京"
}

 Java处理此JSON字符串

 

1、首先将该JSON字符串转换成JSONObject,此处使用的JSONObject为 org.json.JSONObject

 

/**
     * 将jsonString转换成JSONObject
     * @param jsonString
     * @return JSONObject
 */
protected static JSONObject createJsonObject(String jsonString) {
  	if (jsonString != null && !jsonString.isEmpty()) {
  		try {
  			return new JSONObject(jsonString);
  		} catch (JSONException e) {
  			e.printStackTrace();
  		}
	}
  	return null;
  }

 JSONObject object = createJsonObject(json);

 

2、取出JSONObject中关键字为items的值,返回值为JSONArray

 

 JSONArray array = object.getJSONArray("items");

 

通过遍历该数组就可以得到items中的一个一个的对象,通过关键字可以获得对象当中的值

 

if (array.length() != 0) {
	for (int i = 0; i < array.length(); i++) {
	        JSONObject jsonObject = (JSONObject)array.get(i);
		//name即为对象中关键字为name所对应的值
                String name = jsonObject.getString("name");	
	}
}

你可能感兴趣的:(java相关,java,json)