{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
]
people.filter(function (person) {
if(person.dinner){
console.log(person.dinner)
}
if(person.name){
console.log(person.name)
}
})
var people1 = { "name": "bob", "dinner": "pizza" }
Object.keys(people1)
console.log(Object.keys(people1))
var jsonObj = {"1" : 50,"2" : 40,"3" : 40,"4" : 30,"5" : 20,"6" : 20,"7" : 20,"8" : 20,"9" : 20, "10" : 20}
var keyAry = []
var valueAry = []
for(var key in jsonObj){
keyAry.push(key)
valueAry.push(jsonObj[key])
}
console.log(keyAry)
console.log(valueAry)
var obj = {"id": 1, "name":"张三"}
for(var key in obj)
{
alert("Key是:" + key)
alert("对应的值是:" + obj[key])
}
Js遍历json对象所有key及根据动态key获取值:
Js代码
<script type="text/javascript">
getJson('age')
function getJson(key){
var jsonObj={"name":"傅红雪","age":"24","profession":"刺客"}
//1、使用eval方法
var eValue=eval('jsonObj.'+key)
alert(eValue)
//2、遍历Json串获取其属性
for(var item in jsonObj){
if(item==key){ //item 表示Json串中的属性,如'name'
var jValue=jsonObj[item]//key所对应的value
alert(jValue)
}
}
//3、直接获取
alert(jsonObj[''+key+''])
}
</script>
通过上边三种方法就可以取到json中key对应的值。