A. 如何把map轉成json字元串
1、如圖所示新建一個demo作為測試。
B. java將 json數組轉map,例[{"key":"1"},{"key1":"2"}]轉換成一個map
HashMap<String, String> map = new HashMap<String, String>();
String jsonStr="[{\"key\":\"1\"},{\"key1\":\"2\"}]";
JSONArray jsonArr=JSONArray.fromObject(jsonStr);
for(int i=0;i<jsonArr.size();i++){
JSONObject obj = JSONObject.fromObject(jsonArr.get(i));
Iterator it = obj.keys();
while (it.hasNext()){
String key = String.valueOf(it.next());
String value = (String) obj.get(key);
map.put(key, value);
}
}
System.out.println(map);
C. 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);
}
}
D. java在後台如何將前台傳過來的json格式數據轉換為map
你找一個解析json的包,Gson 或者 fastjson ,把你收到的那個字元串 轉換為 JsonObject對象,然後你用Map的操作方式版來操作JsonObject就行了。順便說權下,json的格式比較復雜,它比Map的格式要復雜些,所以,你不可能把json完全轉換為Map,如果格式是按你說的{"a":0,"b":1,"c":1,"d":1} 格式是固定的話,那你自己把JsonObject轉換成Map就可以了
E. 如何根據json對象數組的value獲取對應的key
使用for語句對jsonArray遍歷
<html>
<body>
<scripttype="text/javascript">
varjsonArray=[{"name":"宗2瓜","num":"1","price":"122"}];
//遍歷json數組
for(varobjinjsonArray){
//將json對象轉換為字元串
varstr=JSON.stringify(jsonArray[obj]);
//將json字元串轉換為map
varmap=eval("("+str+")");
//遍歷Map
for(varkeyinmap){
varvalue=map[key];
if(value=='宗2瓜'){
alert('宗2瓜=>key['+key+']');
}
}
}
</script>
</body>
</html>
F. java 中json.parsearray怎麼將結果裝換成list<map<string,string>>
json.parsearray默認是將字元串轉換成json數組,其實就是key-value的形式,然後你new一個list,循環add就可以了。