讀一個JSON文件 遍歷內容 key value轉換

	private String transformNation(String nation){
		String result = "";
		Resource resources = this.resourceLoader.getResource("classpath:static/sion6/cadre/base/resources/store/nation.json");
		String path = resources.toString();
		path = path.replace("\\", "/");
		JSONArray jsonArray = null;
		try {
			String input = FileService.convertStreamToString(resources.getInputStream());
			JSONObject jsonObject = JSONObject.fromObject(input);
			if (jsonObject != null) {
				jsonArray = jsonObject.getJSONArray("data");
				if(jsonArray.size()>0){
					for(int i=0;i<jsonArray.size();i++){
						JSONObject job = jsonArray.getJSONObject(i);  // 遍歷 jsonarray 數組,把每一個對象轉成 json 對象
						if(nation.equals(job.get("name"))){
							result = job.get("text").toString()+"族";
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章