不能在JSON字符串上使用.indexof,因为它没有'indexof'javascript

Can't use .indexof on JSON string because it has no method 'indexof' javascript

本文关键字:indexof javascript 因为 JSON 字符串 不能      更新时间:2023-09-26

我有一些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 () {
    }
});

谢谢你的时间,抱歉如果这有点混乱。

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

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

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