从json变量获取值时的未定义值

时间:2013-02-12 21:08:20

标签: javascript json

我有一个返回json数据的c#webservice。这是要返回的数据。

[ { "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;


}

1 个答案:

答案 0 :(得分:4)

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

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