How can I check if a value is a json object?

后端 未结 13 1434
-上瘾入骨i
-上瘾入骨i 2020-11-30 23:33

My server side code returns a value which is a json object on success and a string \'false\' on failure. Now how can I check whether the returned value is a json object?

13条回答
  •  抹茶落季
    2020-12-01 00:03

    var data = 'json string ?';
    var jdata = null;
    try
    {
        jdata = $.parseJSON(data);  
    }catch(e)
    {}
    
    if(jdata)
    {
    //use jdata
    }else
    {
    //use data
    }
    

提交回复
热议问题