【问题标题】:Get values from JSON returned data从 JSON 返回的数据中获取值
【发布时间】:2015-08-13 16:16:18
【问题描述】:

所以我有一个函数可以进行 ajax 调用并返回一个 json 字符串。我在尝试访问我需要的值时遇到问题,下面是我的代码以及我尝试过的一些示例。

    s.search().then(function (specials) {
        var returnJSON = JSON.parse(specials[0]);
        var x = returnJSON.location.x;
        var y = returnJSON.location.y;
        });

当我检查开发工具时,我收到以下错误。

 JSON.parse: unexpected character at line 1 column 2 of the JSON data

这是我字符串化后的 JSON 返回值。

[{"feature":{"geometry":{"type":"point","x":-82.9172080701955,"y":42.55426092899978,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Postal","Match_addr":"48035, Clinton Township, Michigan","StAddr":"","City":"Clinton Township","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-82.922209,"ymin":42.549261,"xmax":-82.912209,"ymax":42.559261,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"48035, Clinton Township, Michigan"},{"feature":{"geometry":{"type":"point","x":-84.03589825899667,"y":44.826904141314174,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.085899,"ymin":44.776904,"xmax":-83.985899,"ymax":44.876904,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-83.93987906956261,"y":42.065412162742234,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-83.98988,"ymin":42.015412,"xmax":-83.88988,"ymax":42.115412,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-82.93354923650725,"y":42.60054198222781,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-82.98355,"ymin":42.550542,"xmax":-82.88355,"ymax":42.650542,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-83.97095926895429,"y":42.07240087260328,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton, Michigan","StAddr":"","City":"Clinton","score":94.29},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.02096,"ymin":42.022401,"xmax":-83.92096,"ymax":42.122401,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton, Michigan"},{"feature":{"geometry":{"type":"point","x":-84.6015125489642,"y":42.943655651388326,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"SubAdmin","Match_addr":"Clinton, Michigan","StAddr":"","City":"Clinton","score":94.29},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.839514,"ymin":42.705656,"xmax":-84.363514,"ymax":43.181656,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton, Michigan"}]

我正在尝试访问候选位置 x 值和 y 值。

【问题讨论】:

  • JSLint 说那是无效的! (jslint.com)
  • 这似乎很奇怪:"y":42 .072400872603282
  • json 是从映射服务返回的。即使我将它从 JSON.parse 更改为 JSON.stringify 我仍然无法获取我需要的数据
  • 您刚刚更新了您的问题,并且使给出的答案无效!您现在在问题中拥有的 JSON 是有效的。
  • JSON.parseJSON.stringify非常不同的事情,就像相反的不同!为什么要互换使用它们?

标签: javascript jquery json vb.net


【解决方案1】:

您的 JSON 中有一些字符串位于不同的行上。当您将 JSON 复制并粘贴到 linter 中时(例如:json linter),您将看到错误。

编辑: 您编辑了您的问题,因此您现在使用的是有效的 JSON。

如果您已经拥有有效的 JSON,则无需解析您的 JSON。您只需选择正确的键即可。查看您的 JSON,您可以像这样选择您的 xy

var returnJSON = specials[0];
var x = returnJSON.feature.geometry.x;
var y = returnJSON.feature.geometry.y;

codepen 为例。

【讨论】:

  • 中间有两个巨大的 JSON 块,这无助于这个问题/答案的可读性 :( 你可以删除这个答案,因为它不再相关(当然也可以更改它
【解决方案2】:

您通常不需要“解析”从服务返回的 JSON。它已经是 then 回调中的 Javascript 对象。

s.search().then(function (specials) {
    var firstItem = specials[0];
    var x = firstItem.location.x;
    var y = firstItem.location.y;
});

但应注意,根据您发布的 JSON 示例,在您的 specials 数组中返回的第一项不具有属性 location

也许你在寻找firstItem.feature.geometry.x,但我不确定。

【讨论】:

  • 这实际上是行不通的。 firstItem.location 未定义
  • 更新了我的代码,但仍然无法正常工作。 TypeError: firstItem.feature 未定义
  • @pullmyhair 那么您的 json 不是您在问题中向我们展示的内容,因为如果我将您的 json 粘贴到 jsfiddle 并尝试读取该属性,那么它就可以正常工作。 (jsfiddle.net/e0c3g1hm) 你已经从你的代码中删除了所有JSON.parse/JSON.stringify,对吧?
猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 2016-11-25
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2013-09-18
相关资源
最近更新 更多