map.set('key1','value1')
map.set('key2','value2')
// Map 转 JSON字符串
let map_string = JSON.stringify([...map])
console.log('map ->json:',map_string)
// JSON字符串转map
let obj = JSON.parse(map_string)
let new_map = new Map()
for(let key of Object.keys(obj)){
new_map.set(key,obj[key])
}
console.log('json ->map:',new_map)
如果是json字符串可以使用 var jsonObj=JSON.parse(json字符串)解析出来。再遍历如:
var jsonStr='{"classId":1,"className":"前端学习速成班","students":[{"studentId":1,"studentName":"刘德华"},{"studentId":2,"studentName":"张学友"}]}'
var obj=JSON.parse(jsonStr)
alert("我的班级:"+obj.className+",我是,"+obj.students[0].studentName)