2

I am following this link to build a Client side repeater.

According to the above link to retrive the value from a Json key-value pair we can use result[i].property,for example

for (var post in msg)
{     
    var x= msg[post].Date;
    alert(x);
}

Here x returns the date from Json string. When I am trying to use the same with my data, it always returns as undefined. I tried to pop up an alert(msg.d) that shows a nice Json string with all my data. But when I try msg[post].Date(property name) it's always returning undefined.

Please help..

Thanks in advance.

Update:

From the backend I am returning a generic list and then converting it into Json using the following code.

public static string ConvertToJSON(this object obj) {

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());    

    MemoryStream ms = new MemoryStream();
    serializer.WriteObject(ms, obj);
    string jsonobj = Encoding.Default.GetString(ms.ToArray());
    ms.Dispose();
    return jsonobj;
}

then I am appending the returned Json to a stringbuilder and returning it to the Jquery ajax method.The returned Json looks like a string , not an object and hence I am unable to get any value from Json key value pair..

7
  • Could you post what is returned when you call alert(msg.d)? Commented Feb 4, 2010 at 17:11
  • get firebug, place a breakpoint and look at the different values in the watch window. Check what is in post, msg[post]. Commented Feb 4, 2010 at 17:15
  • Hi Patrick, following is a sample of what is returned when I do alert(msg.d) [{"AnsNo":0,"Answer":"","Category":"Help Centre.Mortgages.Existing customers","ClickURL":null,"ID":7,"Question":"How do I re-mortgage to you?","RecNo":0,"ValidFrom":"\/Date(-62135596800000+0000)\/","ValidUntill":"\/Date(-62135596800000+0000)\/"}] so I am trying with msg[post].AnsNo ,msg[post].Answer, msg[post].Category etc Commented Feb 4, 2010 at 17:17
  • I don't understand what this piece of code is trying to do: msg[post].Date(property name). It appears that it will set the Date using property name which is not a date object, but aren't you trying to read the date? Could you post more of your code please Commented Feb 4, 2010 at 17:43
  • 3
    You've asked 8 questions and selected 0 answers. Fix that first and then I will help you. Commented Feb 4, 2010 at 17:49

3 Answers 3

1

Update:

It sounds like you're doubly serializing the data on the server-side, returning a JSON string that gets serialized as JSON itself. When that reaches the client-side and jQuery deserializes the first layer of JSON automatically, you're still left with the JSON string you manually built.

You should return the List<T> as the return value of your service method and let the framework handle JSON serialization.

Original Answer:

Assuming the object is still contained in ASP.NET AJAX's ".d" wrapper, as you implied:

for (var post in msg.d) {     
  var x = msg.d[post].Date;

  alert(x);
}

Or, a traditional loop, which might be more straightforward:

for (var i = 0; i < msg.d.length; i++) {
  var x = msg.d[i].Date;

  alert(x);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Dave,after reading your post I realized that I am double serializing the data.such a silly mistake.Now I'm able to read the value with msg.d[0].Date.But got another problem.basically in the code behind web method I have different conditions.Based on the condition different generic list(ex.faqs,recognitions etc) is returned to the Jquery ajax method.I do not understand how can I set the return type(generic list) for the Web method based on the condition using your solution.Previously I used to append the string returned from Converttojson to a stringbuilder and return.
Use IEnumerable as the return type.
Returning IEnumerable looks like a great idea.In my code I have some conditions which will have to retrive data from multiple generic lists.For example if a condition is satisfied I need to do the following. Recognitionfactory rf = new Recognitionfactory(); Recognitions rs= rf.getBasicSearchRecognitions(searchtext); Answerfactory af = new Answerfactory(); Answers ans = af.getBasicSearchAnswers(searchtext); In the above case I need to return both rs,ans(generic lists) upon satisfying a certain condition.how can I return both of them for one condition?
Ofcourse,It is possible to return either ans or rs with ienumerable being the return type of the web method.how to handle returning both ans,rs
An IEnumerable could contain a collection of IEnumerables. That sounds like it's going to be difficult to work with though. Are you sure you shouldn't refactor it into separate methods? Or, maybe use a common DTO class (e.g. SearchResults) and map the various possible results to a List<SearchResults>?
|
1

What about the message, if you add this to your ajax does that help?

dataFilter: function(data) {
  var msg;
  if (typeof(JSON) !== 'undefined' &&
    typeof(JSON.parse) === 'function')
    msg = JSON.parse(data);
  else
    msg = eval('(' + data + ')');
  if (msg.hasOwnProperty('d'))
    return msg.d;
  else
    return msg;
},

Note that you then process this as msg, not msg.d like so:

success: function(msg) {
  SaveSuccess(msg);
},

Comments

0

Looks to me like you got an array of objects to begin with, using your sample the following works:

messages = [{"AnsNo":0,"Answer":"","Category":"Help Centre.Mortgages.Existing customers","ClickURL":null,"ID":7,"Question":"How do I re-mortgage to you?","RecNo":0,"ValidFrom":"\/Date(-62135596800000+0000)\/","ValidUntill":"\/Date(-62135596800000+0000)\/"}]

for(i in messages)
{
    message = messages[i];
    //Says "Help Centre.Mortgages.Existing customers"
    alert(message.Category)
    //To get all key/value pairs
    for(key in message)
    {
        //Alerts all key value pairs one by one
        alert("Key:" + key + ", Value:" + message[key]);
    }
}

Comments

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.