如何使用jQuery从JSON提取信息 [英] how to extract information from JSON using jQuery

查看:97
本文介绍了如何使用jQuery从JSON提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 JavascriptSerializer类.目前,我从查询中获得了以下JSON:

I have a JSON response that is formatted from my C# WebMethod using the JavascriptSerializer Class. I currently get the following JSON back from my query:

{"d":"[{\"Lat\":\"51.85036\",\"Long\":\"-8.48901\"},{\"Lat\":\"51.89857\",\"Long\":\"-8.47229\"}]"}

我的下面的代码有问题,我希望有人可以对此有所启发.我似乎无法从返回给我的值中获得信息.理想情况下,我希望能够读取返回给我的每一行的纬度和经度值.

I'm having an issue with my code below that I'm hoping someone might be able to shed some light on. I can't seem to get at the information out of the values returned to me. Ideally I would like to be able to read in the Lat and Long values for each row returned to me.

以下是我目前所拥有的:

Below is what I currently have:

$.ajax({
                    type: "POST",
                    url: "page.aspx/LoadWayPoints",
                    data: "{'args': '" + $('numJourneys').val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != '[]') {
                            var lat = "";
                            var long = "";
                            $.each(msg.d, function () {
                                lat = this['Lat'];
                                long = this['Long'];
                            });
                            alert('lat =' + lat + ', long =' + long);
                        }
                    }
                });

我认为问题与JSON的格式有关,但我可能不正确.任何帮助都会很棒.

I think the issue is something to do with how the JSON is formatted but I might be incorrect. Any help would be great.

谢谢, 丰富

推荐答案

正在发生的事情是,您正在JSON内的字符串中对JSON进行编码.这是多余的,但是有一个解决方案无需更改您的输出方法.

What's happening is you're getting JSON encoded inside a string within your JSON. It's redundant, but there is a solution without changing your output method.

要处理此问题,jQuery有一个JSON解析器($.parseJSON()),您可以使用do解析响应中的字符串.

To handle this, jQuery has a JSON parser ($.parseJSON()) you can use do parse the string inside the response.

所以我相信您会这样做:

So I believe you would do it like this:

$.ajax({
                type: "POST",
                url: "page.aspx/LoadWayPoints",
                data: "{'args': '" + $('numJourneys').val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d != '[]') {
                        var content = $.parseJSON(msg.d);

                        var lat = "";
                        var long = "";
                        $.each(content, function () {
                            lat = this['Lat'];
                            long = this['Long'];
                        });
                        alert('lat =' + lat + ', long =' + long);
                    }
                }
            });

这篇关于如何使用jQuery从JSON提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆