如何验证某个字符串是否为JSON?

时间:2022-11-24 22:14:13

I'm working in an application that sends to me a XML so I can work with the data in it. But now the application will send not only a XML, but also a JSON, depending on other variables.

我正在使用一个向我发送XML的应用程序,因此我可以处理其中的数据。但是现在应用程序不仅会发送XML,还会发送JSON,具体取决于其他变量。

Is there any way that I can verify if what has been sent is a JSON? Something equivalent to typeof or instanceof that will tell me that string is a JSON?

有什么办法可以验证发送的是JSON吗?相当于typeof或instanceof的东西会告诉我字符串是JSON吗?

Edit: I'm giving maintenance in this application that has been built using a very, very bad programmed framework. Right now my "data" is the return of a function and I'm unable to get the Content-Type without refactoring good part of the framework - what would take me months - and I don't have this time right now.

编辑:我正在使用一个非常非常糟糕的编程框架构建的应用程序中进行维护。现在我的“数据”是一个函数的返回,我无法在没有重构框架的好部分的情况下获得Content-Type - 这需要几个月的时间 - 而且我现在没有这个时间。

Right now:

ajax.request('POST',function(data){
    xml = loadXML(data); // It's always a XML, so I simply load it.
    ...
    ..
})

What I need:

我需要的:

ajax.request('POST',function(data){
    if(valueCanBeJSON(data)){ // It's not always a XML. How can I do this verification?
        json = eval('('+data+')');
    }else{
        xml = loadXML(data);
        ...
        ..
    }
})

2 个解决方案

#1


1  

As @Gumbo commented, you can check the Content-Type HTTP response header field. Alternately, you could try to parse it — though don't use eval(). Use JSON.parse(). If you're using jQuery, $.parseJSON() or just $.ajax() (without specifying the data-type) will also work.

正如@Gumbo所评论的那样,您可以检查Content-Type HTTP响应头字段。或者,您可以尝试解析它 - 但不要使用eval()。使用JSON.parse()。如果您正在使用jQuery,$ .parseJSON()或只需$ .ajax()(不指定数据类型)也可以。

ajax.request('POST', function(data) {
    var isJSON;
    try {
        data = JSON.parse(data);
        isJSON = true;
    }
    catch (e) {
        isJSON = false;
    }

    if (isJSON) {
        // data is already parsed, so just use it
    }
    else {
        // try treating it as XML
    }
})

#2


0  

If the server is working correctly, it should include a Content-type header that tells you the format of the response body. Also, if it's capable of sending the response as two different formats, then it should try to honor the Accept header in the request, and send the format that the client specifies.

如果服务器工作正常,它应该包含一个Content-type标头,告诉您响应主体的格式。此外,如果它能够以两种不同的格式发送响应,那么它应该尝试遵守请求中的Accept标头,并发送客户端指定的格式。

I suggest you take a look at JQuery - it has facilities to handle this sort of thing.

我建议你看看JQuery - 它有处理这类事情的工具。

#1


1  

As @Gumbo commented, you can check the Content-Type HTTP response header field. Alternately, you could try to parse it — though don't use eval(). Use JSON.parse(). If you're using jQuery, $.parseJSON() or just $.ajax() (without specifying the data-type) will also work.

正如@Gumbo所评论的那样,您可以检查Content-Type HTTP响应头字段。或者,您可以尝试解析它 - 但不要使用eval()。使用JSON.parse()。如果您正在使用jQuery,$ .parseJSON()或只需$ .ajax()(不指定数据类型)也可以。

ajax.request('POST', function(data) {
    var isJSON;
    try {
        data = JSON.parse(data);
        isJSON = true;
    }
    catch (e) {
        isJSON = false;
    }

    if (isJSON) {
        // data is already parsed, so just use it
    }
    else {
        // try treating it as XML
    }
})

#2


0  

If the server is working correctly, it should include a Content-type header that tells you the format of the response body. Also, if it's capable of sending the response as two different formats, then it should try to honor the Accept header in the request, and send the format that the client specifies.

如果服务器工作正常,它应该包含一个Content-type标头,告诉您响应主体的格式。此外,如果它能够以两种不同的格式发送响应,那么它应该尝试遵守请求中的Accept标头,并发送客户端指定的格式。

I suggest you take a look at JQuery - it has facilities to handle this sort of thing.

我建议你看看JQuery - 它有处理这类事情的工具。