Home >Web Front-end >JS Tutorial >Method to convert JSON string into Map object

Method to convert JSON string into Map object

高洛峰
高洛峰Original
2016-12-03 14:35:482499browse

The page passes a json string to the background action, and the json string needs to be converted into a Map object

public Map<String, String> toMap(Object object) {
  Map<String, String> data = new HashMap<String, String>();
  // 将json字符串转换成jsonObject
  JSONObject jsonObject = JSONObject.fromObject(object);
  Iterator ite = jsonObject.keys();
  // 遍历jsonObject数据,添加到Map对象
  while (ite.hasNext()) {
    String key = ite.next().toString();
    String value = jsonObject.get(key).toString();
    data.put(key, value);
  }
  // 或者直接将 jsonObject赋值给Map
  // data = jsonObject;
  return data;
}

The above method of converting a JSON string into a Map object is all the content shared by the editor.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn