如何获取部分 JSON 字符串

How to grab part of JSON string?

本文关键字:JSON 字符串 获取 何获取      更新时间:2023-09-26
            this.on('success', function(file, responseText) { 
                var theID = JSON.stringify(responseText);
                alert(theID);
                window.location.href = ('want to put something here');    
            });

警报给我:

{"消息":"成功","文件 ID":399}

我想获取 fileID 值,在本例中为 399。谢谢。

window.location.href = responseText.fileID;

会做,因为响应文本已经是 JSON

this.on('success', function(file, responseText) { 
            var theID = JSON.stringify(responseText);
            alert(theID);
            if(responseText.fileID == 399) {
                // Do something
            }
            window.location.href = ('want to put something here');    
        });

如果 responseText 已经是 JSON 格式,则 :

   responseText.fileID

如果是字符串格式,则首先解析它:

var json = JSON.parse(responseText);
alert(json.fileID);