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 oject=createJson Object(json);
 
2、JSONObjectのキーワードがitemsの値を取り出し、戻り値はJSONArrayです。
 
 JSONArray array=object.get JSONArray(“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");	
	}
}