【问题标题】:Can't use .indexof on JSON string because it has no method 'indexof' javascript无法在 JSON 字符串上使用 .indexof,因为它没有方法 'indexof' javascript
【发布时间】:2011-08-18 13:34:09
【问题描述】:

我解析了一些 JSON,但我想从 JSON 中获取一个链接,在链接中找到一个使其不同的标识符(例如 www.foo.com/IDENTIFIER/home),并拥有该标识符作为一个字符串插入到另一个使用标识符的函数中。

这是我尝试使用 indexOf 查找预标识符路径索引的代码,因此我可以在它之后获取标识符的索引(该代码尚未编写,因为我被困在这里)。这也使用 JSONP。

$.jsonp({
    "url": "http://foo.com&callback=?",
    "data": {
    "alt": "json-in-script"
    },
    "success": function (data) {
        var link = data[0].link;
        var jsonText = JSON.stringify(link);

        // the below code returns string
        console.log('jsonText string?'+typeof jsonText); 

        // the below code returns that this method has no object indexOf
        var index = jsonText.indexof("pre-identifier-path/"); 

    },
    "error": function () {
    }
});

感谢您的宝贵时间,如果这有点令人困惑,抱歉。

【问题讨论】:

    标签: javascript json jsonp


    【解决方案1】:

    该方法应命名为indexOf,区分大小写。例如:

    jsonText.indexOf("pre-identifier-path/"); 
    

    【讨论】:

      【解决方案2】:

      函数是indexOf(),即大写O-区分大小写。

      【讨论】:

      • 哇,我有点尴尬,我没听懂,但非常感谢。这解决了它。
      • 这发生在我们最好的人身上。 :)
      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多