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;


}
user541597
  • 4,247
  • 11
  • 59
  • 87
  • 1
    `console.log(list, arg);` --- for debugging purposes use `console.log`, not `alert` – zerkms Feb 12 '13 at 21:14
  • possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling Feb 12 '13 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. – Felix Kling Feb 12 '13 at 21:17
  • @Felix Kling: then the `Convert2Json.Convert` call looks odd. Though your comment makes much sense – zerkms Feb 12 '13 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. – Felix Kling Feb 12 '13 at 21:19
  • @Felix Kling: I don't know either, but the name... ) – zerkms Feb 12 '13 at 21:20
  • edited to show what convert2json does – user541597 Feb 12 '13 at 21:51
  • @user541597: you've shown us C# code. How is it related to js? :-S – zerkms Feb 12 '13 at 21:55
  • I'd be inclined to agree with @Felix King; "arg" musn't actually an array when you receive it in OnComplete. [link]http://jsfiddle.net/WCNhN/ (note how the second "array" is actually a string so you get undefined). Try changing your C# to return List and let the built-in serialiser take care of it for you... – Stephen Byrne Feb 12 '13 at 22:14

1 Answers1

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.

zerkms
  • 249,484
  • 69
  • 436
  • 539