开发者

Object XMLHttpRequest to JSON

I am creating an jquery ajax form which calls the method below

public string GetRestaurantInfo(string date, string pageId)
{
    Node node = new Node(Convert.ToInt32(pageId));
    string day = DateTime.Parse(date).DayOfWeek.ToString();
    return JsonConvert.SerializeObject(GetOpeningHours(node, day));
}

private static object GetOpeningHours(Node node, string day)
{
    XDocument xmlDoc = XDocument.Parse(node.GetProperty("openingHours").ToString());
    var q = from item in xmlDoc.Descendants("scheduleItem")
                where item.Element("weekDayLocal").Value == day
                select new
                {
                    day = item.Element("weekDayLocal").Value,
                    startTime = item.Element("firstSet").Element("hourStart").Value,
                    closingTime = item.Element("firstSet").Element("hourEnd").Value,
                    hoursOpen = 4
                };
    return q;
}

I would like the data to be returned in a JSON format, but it is returning the data in the following format

{"d":" [{\"day\":\"Tuesday\"开发者_JAVA技巧,\"startTime\":\"17:00\",\"closingTime\":\"11:00\",\"hoursOpen\":4}]"}

I am not sure how to resolve this? Any ideas?

Thanks in advance for any help


I assume this thread was not answered, and I found this thread when I Google as I faced the same issue too. After a struggle with Firebug, The solution was simple in the end. You just have to parse it twice as in the following code. But I am not sure whether this is the correct solution, or is this an impact in the web service call that I tried to make.

JSON.parse(JSON.parse(result).d)

Anyway just for some one who want to know the web service call,

    xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                //xhr.responseText; this contains the data
            }
        }
    };
    xhr.send(params);

Thanks, Sabo


Well that is jSon i suppose. Did you try doing below in the callback javascript function.

function callback(rslt,cntxt){
var result = Sys.Serialization.JavaScriptSerializer.deserialize(rslt);
console.dir(result);
}

watch the firebug console and inspect the object that was dumped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜