欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json遍历

程序员文章站 2022-07-12 15:51:11
...

遍历json对象:

无规律:

<script>
var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}];
for(var i=0,l=json.length;i<l;i++){
for(var key in json[i]){
alert(key+':'+json[i][key]);
}
}
</script>

?

有规律:

packJson = [

{"name":"nikita", "password":"1111"},
 
{"name":"tony", "password":"2222"}
 
];
 
for(var p in packJson){//遍历json数组时,这么写p为索引,0,1
 
  alert(packJson[p].name + " " + packJson[p].password);
 
}

也可以用这个:

?
for(var i = 0; i < packJson.length; i++){
 
  alert(packJson[i].name + " " + packJson[i].password);
 
}
遍历json对象
?
myJson = {"name":"caibaojian", "password":"1111"};
 
for(var p in myJson){//遍历json对象的每个key/value对,p为key
 
  alert(p + " " + myJson[p]);
 
}
java 解析 json 遍历未知key
json遍历
1、——————————————————————————————————————————————————————————————
import net.sf.json.JSONObject; String json = "{\"name\":\"lss\"}"; JSONObject jsonObj = JSONObject.fromObject(json); String name = jsonObj.getString("name"); Iterator it = jsonObj.keys(); List<String> keyListstr = new ArrayList<String>(); while(it.hasNext()){ key = (String) it.next();
     value = jsonObject.getString(key);
}
json遍历

 

2、 {"info":[{"goodsId":"1234","goodsq":"10"},{"goodsId":"5678","goodsq":"20"}]}

json遍历
2、———————————————————————————————————————————————————————————————
JSONObject jsonObject
= new JSONObject(jsonString);   JSONArray jsonArray =jsonObject.getJSONArray(“info”);   for (int i = 0; i < jsonArray.length(); i++) {   JSONObject jo = jsonArray.getJSONObject(i);        System.out.println(jo.getString("goodsld"));        System.out.println(jo.getString("goodsq")); }
json遍历