本文目錄一覽:
怎樣用for循環動態遍歷json數組
最少你說下語言吧,都不說下語言怎麼回答?
如果是js,數組直接for循環,然後如果是json對象,而你不知道key的話,採用
for(var i in json){
console.log(‘key:’ + i +”;value:” + json[i]);
}
json數據請問怎麼遍歷
如果是js中遍歷使用
var anObject = {one:1,two:2,three:3};//對json數組each
$.each(anObject,function(name,value) {
});
如果是Java代碼直接用for循環就行了,說白了json也是數組的一種,json對象和json數組都可以
//遍歷json數組
String json1 = “{data:[{name:’Wallace’},{name:’Grommit’}]}”;
jsonObjSplit = new JSONObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray(“data”);
for (int i = 0; i ja.length(); i++) {JSONObject jo = (JSONObject) ja.get(i);System.out.println(jo.get(“name”));}
//JSONObject遍歷json對象
String json2 = “{name:’Wallace’,age:15}”;
jsonObj = new JSONObject(json2);
for (Iterator iter = jsonObj.keys(); iter.hasNext();) {String key = (String)iter.next();System.out.println(jsonObj .getString(Key));}
ajax中如何把傳遞過來的json數據循環遍歷出
先把傳遞過來的字元串轉換成對象,再按照一般思路遍歷就可以了。
var jsonObj = eval(“(“+xmlHttp.responseText+”)”);//轉換為對象
for(var i=0;ijsonObj.length;i++){…}
對象裡面有啥屬性,按照後台的操作正常取出來就可以了。假如對象jsonObj
裡面含有後台的Person對象(並且含有屬性:private String name;),遍歷的時候獲取name的話,可以
for(var i=0;ijsonObj.length;i++){
var personName = jsonObj[i].name;
}
稍微提示下,希望有所助!
ASP讀取JSON數組的問題。求解啊!!!!
script language=”JScript” runat=”Server”
function ToObject(json) {
var o;
eval(“o=” + json);
return o;
}
function toArray(s){
var dic = Server.CreateObject(“Scripting.Dictionary”)
eval(“var a=” + json);
for(var i=0;ia.length;i++){
var obj = Server.CreateObject(“Scripting.Dictionary”)
for(x in a[i]) obj.Add(x,a[i][x])
dic.Add(i, obj);
}
return dic
}
/script
%
json = “[{“”date””:””周四 08月07日 (實時:2)””,””weather””:””晴””,””wind””:””微風””,””temperature””:””21″”},{“”date””:””周五””,””weather””:””多雲””,””wind””:””微風””,””temperature””:””31 ~ 22″”},{“”date””:””周六””,””weather””:””多雲轉陰””,””wind””:””微風””,””temperature””:””30 ~ 22″”},{“”date””:””周日””,””weather””:””陰轉晴””,””wind””:””微風””,””temperature””:””31 ~ 22″”}]”
Set ob = toArray(json)
For i=0 To ob.Count-1
Response.Write ob(i)(“date”) ” br/”
next
Set ob = Nothing
%
注意JSON字元串前後的 [ ]
原創文章,作者:RRKZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/142722.html