javascript - 从 json 变量获取值时出现未定义值

标签 javascript json

我有一个返回 json 数据的 C# Web 服务。这是正在返回的数据。

[ { "destination": "pandora.com", "hits": 9 }, 
{ "destination": "google.com", "hits": 2 }, 
{ "destination": "msftncsi.com", "hits": 2 }, 
{ "destination": "nmlsconsumeraccess.org", "hits": 1 }, 
{ "destination": "facebook.com", "hits": 1 }, 
{ "destination": "gravatar.com", "hits": 1 }, 
{ "destination": "iheart.com", "hits": 1 }, 
{ "destination": "kiss1041fm.com", "hits": 1 }, 
{ "destination": "live.com", "hits": 1 }, 
{ "destination": "microsoft.com", "hits": 1 }, 
{ "destination": "today.com", "hits": 1 }, 
{ "destination": "update.microsoft.com", "hits": 1 }, 
{ "destination": "xsitesnetwork.com", "hits": 1 }, 
{ "destination": "youtube-nocookie.com", "hits": 1 }, 
{ "destination": "youtube.com", "hits": 1 }, 
{ "destination": "zillow.com", "hits": 1 } ]

这是我的 JavaScript:

ret = Convert2Json.Convert(OnComplete, OnTimeOut, OnError); //this is my webservice

function OnComplete(arg) {

    $('#label').text(arg); //when I set this label to arg
    //I get the json data correctly as above

    var list = {
        "entries": arg
    };

    alert(list.entries[0].destination);
    //this gives me undefined when it popups
}

function OnTimeOut(arg) {
    alert("TimeOut encountered when calling server");
}

function OnError(arg) {
    alert("Error encountered when calling server");
}

如果我自己定义列表如下,它就可以工作:

var list = { "entries": [{ "destination": "pandora.com", "hits": 9 }, { "destination": "google.com", "hits": 2 }, { "destination": "youtube.com", "hits": 2 }, { "destination": "facebook.com", "hits": 1 }, { "destination": "fdic.gov", "hits": 1 }, { "destination": "GOV_new", "hits": 1 }, { "destination": "iheart.com", "hits": 1 }, { "destination": "jcpportraits.com", "hits": 1 }, { "destination": "kiss1041fm.com", "hits": 1 }, { "destination": "live.com", "hits": 1 }, { "destination": "msftncsi.com", "hits": 1 }, { "destination": "publix.com", "hits": 1 }, { "destination": "today.com", "hits": 1 }, { "destination": "xsitesnetwork.com", "hits": 1 }, { "destination": "youtube-nocookie.com", "hits": 1 }] };

这是 Convert2Json.Convert 的作用

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Convert() {




    using (SqlConnection conn = new SqlConnection("Server=localhost;Initial Catalog=Testing_Server;Trusted_Connection=True;"))
    {

        SqlCommand cmd = new SqlCommand("SELECT Destination as destination, count(distinct(source)) as hits FROM [Carlos_Test] where GETDATE() < DATEADD(MINUTE,5,time) and destination not in ('google-analytics.com','gstatic.com','googleadservices.com','download.windowsupdate.com') group by Destination order by hits DESC", conn);




        conn.Open();


        List<VisitedSites> slist = new List<VisitedSites>();

        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read()) {



            VisitedSites vs = new VisitedSites();
            vs.destination = reader["destination"].ToString();
            vs.hits = Int32.Parse(reader["hits"].ToString());

            slist.Add(vs);

        }





        string jSon = JsonConvert.SerializeObject(slist, Formatting.Indented);


        return jSon;
    }



}

public class VisitedSites {

    public string destination;
    public int hits;


}

最佳答案

根据您所显示的内容 - arg 是一个数组。

所以,说 alert(list.entries[0].destination); 可以工作,给你第一个元素,但是 alert(list.entries.destination); code> 不会,因为 list.entries 是一个数组,没有指定 .destination 属性。

关于javascript - 从 json 变量获取值时出现未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841862/

相关文章:

javascript - 避免 Redis 持久化(Socket.io + Cluster + RedisStore)

json - 我如何使用 Go 漂亮地打印 JSON?

python - Flask:是否可以返回一个集合作为 JSON 响应

json - 空注入(inject)器错误 : No provider for JwtHelperService

javascript - 使用 noConflict 方法后下划线未定义

javascript - 插入 DOM 的 jQuery 不跨浏览器兼容

javascript - 在ajax加载的div中注入(inject)脚本

javascript - dc.js - 如何按唯一 ID 分组

Python:如何从 JSON 文件中提取参数?

javascript - 使用 $.each 将值放入数组对象中