0

I have a c# webservice that returns json data. Here is the data that is being returned.

[ { "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 } ]

Here is my 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");
}

If I define list myself as follows, it works:

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 }] };

Here is what Convert2Json.Convert does

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


}
9
  • 1
    console.log(list, arg); --- for debugging purposes use console.log, not alert Commented Feb 12, 2013 at 21:14
  • possible duplicate of I have a nested data structure / JSON, how can I access a specific value? Commented Feb 12, 2013 at 21:16
  • 1
    You might have to parse the JSON (arg) first. If $('#label').text(arg); shows you the data as you posted it, then arg is a string, not an array. Commented Feb 12, 2013 at 21:17
  • @Felix Kling: then the Convert2Json.Convert call looks odd. Though your comment makes much sense Commented Feb 12, 2013 at 21:19
  • 1
    @zerkms: Well, I don't know what Convert2Json.Convert does. I have seen many odd things ;) One time I stumbled upon a library that had a method called toJSON, which returned a JavaScript object. Commented Feb 12, 2013 at 21:19

1 Answer 1

4

According to what you shown - the arg is an array.

So, say alert(list.entries[0].destination); could work, giving you the first element, but the alert(list.entries.destination); won't because list.entries is an array, that doesn't have .destination property specified.

Sign up to request clarification or add additional context in comments.

3 Comments

edited code. list.entries[0].destination is what I am using. That is what is giving me undefined.
@user541597: without console.log results (see my first comment to the question) it's not possible to say anything else for sure
@user541597: Did you parse the JSON like I suggested?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.