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就可以了。