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"}
这里面有多个date和weather等值
请问我怎么用ASP循环出来这些值呢?
我现在是这样做的
<script>
function toObject(json) {
eval("var o=" + json);
return o;
}
</script>
<%
Dim json
json = "上面那些JSON数组"
Set json = toObject(json)
Response.Write json.date ‘这里目前只能读出到一个DATE
Set json = Nothing
%>
<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;i<a.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") & " " next Set ob = Nothing %>
注意JSON字符串前后的 [ ]