javajsontomap
① java中將map轉成json時,如何將map中的整型數字在轉成json後,變成字元串,而不是整型。
好像沒有什麼特別的辦法,可能是我才疏學淺,
我知道的兩種方式:
map是鍵值對存在,那麼類型都是固定的,我們可以再申請個map<String ,String>遍歷替換原來的map再轉換成json字元串
字元串替換,用正則添加雙引號:
publicvoidtestJson(){
Map<String,Integer>map=newHashMap<String,Integer>();
map.put("aaa",111);
Stringjson=JSON.toJSONString(map);
Stringjson1=json.replaceAll(":",":"");
json1=json1.replaceAll("}",""}");
System.out.println(json);
System.out.println(json1);
}
② java在後台如何將前台傳過來的json格式數據轉換為map
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importjava.util.Map;
/**
*JSON字元串自動轉換
*Createdbyzknon2016/8/22.
*/
publicclassJsonToMapTest01{
publicstaticvoidmain(String[]args){
Stringstr="{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}";
//第一種方式
Mapmaps=(Map)JSON.parse(str);
System.out.println("這個是用JSON類來解析JSON字元串!!!");
for(Objectmap:maps.entrySet()){
System.out.println(((Map.Entry)map).getKey()+""+((Map.Entry)map).getValue());
}
//第二種方式
MapmapTypes=JSON.parseObject(str);
System.out.println("這個是用JSON類的parseObject來解析JSON字元串!!!");
for(Objectobj:mapTypes.keySet()){
System.out.println("key為:"+obj+"值為:"+mapTypes.get(obj));
}
//第三種方式
MapmapType=JSON.parseObject(str,Map.class);
System.out.println("這個是用JSON類,指定解析類型,來解析JSON字元串!!!");
for(Objectobj:mapType.keySet()){
System.out.println("key為:"+obj+"值為:"+mapType.get(obj));
}
//第四種方式
/**
*JSONObject是Map介面的一個實現類
*/
Mapjson=(Map)JSONObject.parse(str);
System.out.println("這個是用JSONObject類的parse方法來解析JSON字元串!!!");
for(Objectmap:json.entrySet()){
System.out.println(((Map.Entry)map).getKey()+""+((Map.Entry)map).getValue());
}
//第五種方式
/**
*JSONObject是Map介面的一個實現類
*/
JSONObjectjsonObject=JSONObject.parseObject(str);
System.out.println("這個是用JSONObject的parseObject方法來解析JSON字元串!!!");
for(Objectmap:json.entrySet()){
System.out.println(((Map.Entry)map).getKey()+""+((Map.Entry)map).getValue());
}
//第六種方式
/**
*JSONObject是Map介面的一個實現類
*/
MapmapObj=JSONObject.parseObject(str,Map.class);
System.out.println("這個是用JSONObject的parseObject方法並執行返回類型來解析JSON字元串!!!");
for(Objectmap:json.entrySet()){
System.out.println(((Map.Entry)map).getKey()+""+((Map.Entry)map).getValue());
}
StringstrArr="{{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"},"+
"{"00":"zhangsan","11":"lisi","22":"wangwu","33":"maliu"}}";
//JSONArray.parse()
System.out.println(json);
}
}
③ java將 json數組轉map
首先你的[{"key":"1"},{"key":"2"}] 是個 json 數組格式
如果是簡單的json 格式, 比如"{"key1":"1","key2":"2"}"
那麼你可以使用 下面的示例:
publicstaticvoidmain(String[]args){
StringjsonString="{"key1":"1","key2":"2"}";
//Stringstr="[{"key1":"1"},{"key2":"2"}]";
Map<String,Object>map=toMap(jsonString);
for(Map.Entry<String,Object>entry:map.entrySet()){
Stringkey=entry.getKey();
Objectvalue=entry.getValue();
System.out.println(key+":"+value);
}
}
@SuppressWarnings("unchecked")
publicstatic<T>Map<String,T>toMap(Stringjson){
Map<String,T>map=newHashMap<String,T>();
JSONObjectjsonObject=JSONObject.fromObject(json,newJsonConfig());
Iterator<String>keys=jsonObject.keys();
while(keys.hasNext()){
Stringkey=keys.next();
Objectvalue=jsonObject.get(key);
map.put(key,(T)value);
}
returnmap;
}
執行結果:
你這種數組格式, 轉成 map 那麼 同名的key 會覆蓋的....
④ JAVA中json字元串如何轉化為map對象獲取數據
我們需要先把json字元串轉化為net.sf.json.JSONObject對象,java中這樣就可以完成json字元串到Map的轉換了。
⑤ java怎麼將json文件讀取進來並轉成map
java中解析json文件,需要下載json解析包,用JSONUtil.deserialize()就可以了,範例:
importorg.apache.struts2.json.JSONUtil;
publicclassJsonToJava{
publicstaticvoidmain(String[]args){
try{
Strings=JSONObject.toString();
Objecto=JSONUtil.deserialize(s);
Objecto1=((HashMap)o).get("DATA");//此舉將DATA作為對象,得到。
Mapmap=(Map)o1;//然後強轉o1
}catch(Exceptione){
e.printStackTrace(System.out);
}
}
}