可以使用递
归函数来实 现JSON 转Map的 功能。需要 注意的是, Map的k ey不能重 复。以下是 一个示例代 码```p ublic stat ic Ma p ana lysis Json2 ALL(O bject objJ son, Map m ap, S tring k) { if ( objJs on in stanc eof J SONOb ject) { JS ONObj ect j sonOb ject = (JS ONObj ect) objJs on; f or (S tring key : jso nObje ct.ke ySet( )) { Objec t val ue = jsonO bject .get( key); if ( value inst anceo f JSO NArra y) { JSONA rray jsonA rray = (JS ONArr ay) v alue; for (int i = 0 ; i < json Array .size (); i ++) { anal ysisJ son2A LL(js onArr ay.ge t(i), map, k + . + k ey + [ + i + ]) ; } } else if ( value inst anceo f JSO NObje ct) { anal ysisJ son2A LL(va lue, map, k + . + ke y); } else { ma p.put (k + . + k ey, S tring .valu eOf(v alue) ); } } } e lse i f (ob jJson inst anceo f JSO NArra y) { JSONA rray jsonA rray = (JS ONArr ay) o bjJso n; fo r (in t i = 0; i < js onArr ay.si ze(); i++) { an alysi sJson 2ALL( jsonA rray. get(i ), ma p, k + [ + i + ]); } } re turn map;} ```该函 数可以处理 数组和对象 混合的JS ON,并返 回一个包含 全量节点的 Map。小编还为您整理了以下内容,可能对您也有帮助:
android怎么把json转换为hashmap
在android中把json转换为hashmap,代码如下:
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import com.wideyou.model.ExpressOrder;
import com.wideyou.warehouse.model.Storage;
public class HelperFunction {
public ExpressOrder ExpressOrderClassCast(JSONObject reserJson){
ExpressOrder expressOrder=new ExpressOrder();
expressOrder.setCid(Integer.parseInt(reserJson.getString("userId")));
expressOrder.setSendPerson(reserJson.getString("sendPerson"));
expressOrder.setSendPhone(reserJson.getString("sendPhone"));
expressOrder.setSendAddress(reserJson.getString("sendAddress"));
expressOrder.setBeginAddress(reserJson.getString("beginAddress"));
expressOrder.setOtherDes(reserJson.getString("otherDes"));
expressOrder.setEndAddress(reserJson.getString("endAddress"));
expressOrder.setReservPerson(reserJson.getString("reservPerson"));
expressOrder.setReservPhone(reserJson.getString("reservPhone"));
expressOrder.setReservAddress(reserJson.getString("reservAddress"));
expressOrder.setPostCode(reserJson.getString("reservAddress"));
expressOrder.setPayId(Integer.parseInt(reserJson.getString("payId")));
return expressOrder;
}
public Storage StorageClassCast(JSONObject reserJson){
Storage storage=new Storage();
storage.setSname(reserJson.getString("sname"));
storage.setCount(Integer.parseInt(reserJson.getString("count")));
return storage;
}
public static int getCid(JSONObject reserJson, boolean isConsumer){
if(isConsumer){
return Integer.parseInt(reserJson.getString("userId"));
}else{
return Integer.parseInt(reserJson.getString("memberId"));
}
}
//map转换为json字符串
public static String hashMapToJson(HashMap map) {
String string = "{";
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Entry e = (Entry) it.next();
string += "'" + e.getKey() + "':";
string += "'" + e.getValue() + "',";
}
string = string.substring(0, string.lastIndexOf(","));
string += "}";
return string;
}
public static void JsonObject2HashMap(JSONObject jo, List> rstList) {
for (Iterator keys = jo.keys(); keys.hasNext();) {
try {
String key1 = keys.next();
System.out.println("key1---" + key1 + "------" + jo.get(key1)
+ (jo.get(key1) instanceof JSONObject) + jo.get(key1)
+ (jo.get(key1) instanceof JSONArray));
if (jo.get(key1) instanceof JSONObject) {
JsonObject2HashMap((JSONObject) jo.get(key1), rstList);
continue;
}
if (jo.get(key1) instanceof JSONArray) {
JsonArray2HashMap((JSONArray) jo.get(key1), rstList);
continue;
}
System.out.println("key1:" + key1 + "----------jo.get(key1):"
+ jo.get(key1));
json2HashMap(key1, jo.get(key1), rstList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public static void JsonArray2HashMap(JSONArray joArr,
List> rstList) {
for (int i = 0; i < joArr.size(); i++) {
try {
if (joArr.get(i) instanceof JSONObject) {
JsonObject2HashMap((JSONObject) joArr.get(i), rstList);
continue;
}
if (joArr.get(i) instanceof JSONArray) {
JsonArray2HashMap((JSONArray) joArr.get(i), rstList);
continue;
}
System.out.println("Excepton~~~~~");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public static void json2HashMap(String key, Object value,
List> rstList) {
HashMap map = new HashMap();
map.put(key, value);
rstList.add(map);
}
}