标签:
JAVA解析json字符串:根据城市itemName名称获取其码itemCode
1、json字符串
[
{"childList":
[{"childList":
[{"childList":[],"id":374,"itemCode":"110101","itemName":"东城区","itemNameEn":"DONGCHENGQU","parentId":35},
{"childList":[],"id":375,"itemCode":"110102","itemName":"西城区","itemNameEn":"XICHENGQU","parentId":35},
{"childList":[],"id":376,"itemCode":"110105","itemName":"朝阳区","itemNameEn":"CHAOYANGQU","parentId":35},
{"childList":[],"id":377,"itemCode":"110106","itemName":"丰台区","itemNameEn":"FENGTAIQU","parentId":35},
{"childList":[],"id":378,"itemCode":"110107","itemName":"石景山区","itemNameEn":"SHIJINGSHANQU","parentId":35},
{"childList":[],"id":379,"itemCode":"110108","itemName":"海淀区","itemNameEn":"HAIDIANQU","parentId":35},
{"childList":[],"id":380,"itemCode":"110109","itemName":"门头沟区","itemNameEn":"MENTOUGOUQU","parentId":35},
{"childList":[],"id":381,"itemCode":"110111","itemName":"房山区","itemNameEn":"FANGSHANQU","parentId":35},
{"childList":[],"id":382,"itemCode":"110112","itemName":"通州区","itemNameEn":"TONGZHOUQU","parentId":35},
{"childList":[],"id":383,"itemCode":"110113","itemName":"顺义区","itemNameEn":"SHUNYIQU","parentId":35},
{"childList":[],"id":384,"itemCode":"110114","itemName":"昌平区","itemNameEn":"CHANGPINGQU","parentId":35},
{"childList":[],"id":385,"itemCode":"110115","itemName":"大兴区","itemNameEn":"DAXINGQU","parentId":35},
{"childList":[],"id":386,"itemCode":"110116","itemName":"怀柔区","itemNameEn":"HUAIROUQU","parentId":35},
{"childList":[],"id":387,"itemCode":"110117","itemName":"平谷区","itemNameEn":"PINGGUQU","parentId":35},
{"childList":[],"id":388,"itemCode":"110228","itemName":"密云县","itemNameEn":"MIYUNXIAN","parentId":35},
{"childList":[],"id":389,"itemCode":"110229","itemName":"延庆县","itemNameEn":"YANQINGXIAN","parentId":35}],
"id":35,"itemCode":"110100","itemName":"北京市","itemNameEn":"BEIJING","parentId":1}],
"id":1,"itemCode":"110000","itemName":"北京市","itemNameEn":"BEIJING","parentId":0}
]
2、方法:
private static String citystr = "json字符串(上面)";
//json字符串转map
public static Map<String, Object> JsonToMap(JSONObject json) {
Map<String, Object> columnValMap = new HashMap<String, Object>();
Set<String> jsonKeys = json.keySet();
for (Object key : jsonKeys) {
Object JsonValObj = json.get(key);
if (JsonValObj instanceof JSONArray) {
columnValMap.put((String) key,JsonToList((JSONArray) JsonValObj));
} else if (JsonValObj instanceof JSONObject) {
columnValMap.put((String) key,JsonToMap((JSONObject) JsonValObj));
} else {
columnValMap.put((String) key, JsonValObj);
}
}
return columnValMap;
}
public static Object JsonToList(JSONArray jsonArr) {
List<Object> jsonObjList = new ArrayList<Object>();
for (Object obj : jsonArr) {
if (obj instanceof JSONArray) {
jsonObjList.add(JsonToList((JSONArray) obj));
} else if (obj instanceof JSONObject) {
jsonObjList.add(JsonToMap((JSONObject) obj));
} else {
jsonObjList.add(obj);
}
}
return jsonObjList;
}
/**
*
* Description:得到开户省code
*
* @param
* @return String
* @throws
* @Author liujianguang
* Create Date: 2015年5月11日 下午2:59:18
*/
public static String getProvinceCode(String provinceName){
String result = "";
JSONArray obj = (JSONArray) JSONArray.parse(citystr);
for(int i=0;i<obj.size();i++){
Map<String, Object> dataMap = JsonToMap(obj.getJSONObject(i));
if(((String)dataMap.get("itemName")).equals(provinceName)&&dataMap.get("parentId").equals(0)){
result = (String)dataMap.get("itemCode");
}
}
return result;
}
/**
*
* Description:得到开户市
*
* @param
* @return String
* @throws
* @Author liujianguang
* Create Date: 2015年5月11日 下午3:14:51
*/
public static String getCityCode(String cityName){
String result = "";
JSONArray obj = (JSONArray) JSONArray.parse(citystr);
for(int i=0;i<obj.size();i++){
Map<String, Object> dataMap = JsonToMap(obj.getJSONObject(i));
@SuppressWarnings("unchecked")
List<Map<String, Object>> cityListMap = (List<Map<String, Object>>)dataMap.get("childList");
for(int n = 0;n<cityListMap.size();n++){
if((cityListMap.get(n).get("itemName")).equals(cityName)){
result = (String)cityListMap.get(n).get("itemCode");
}
}
}
return result;
}
public static void main(String[] args){
System.out.println(getProvinceCode("北京市"));
}
标签:
原文地址:http://www.cnblogs.com/bailuobo/p/4498012.html