简体   繁体   English

将html中的JSON字符串转换为JSON对象

[英]Convert JSON string in html into JSON object

I've retrieved some data in JSON format from MarkitOnDemand API, the JSON content I want is inside the body tag of the html string like this: 我从MarkitOnDemand API检索了JSON格式的一些数据,我想要的JSON内容在html字符串的body标签内,如下所示:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery Autocompelete</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
</head>
<body>
{"Status":"SUCCESS","Name":"Apple Inc","Symbol":"AAPL","LastPrice":109.59,"Change":1.91,"ChangePercent":1.77377414561664,"Timestamp":"Wed Mar 30 15:59:00 UTC-04:00 2016","MSDate":42459.6659722222,"MarketCap":607630850970,"Volume":3211276,"ChangeYTD":105.26,"ChangePercentYTD":4.11362340870226,"High":110.41,"Low":108.6,"Open":108.64}</body>
</html>

The above code of html string is in the "data" string of my AJAX call below: 上面的html字符串代码位于我的AJAX调用的“data”字符串中:

$(function(){
    $('#searchform').on('submit', function(event){
        event.preventDefault();

        var requestdata = 'symbol=' + $('#query').val();
        $.ajax({
            url: "receivesearch.php",
            method: "get",
            data: requestdata,
            success: function(data){  //html string in this data parameter

                //CONFUSED HERE

            }
        });
    });
});

But I failed to get the JSON string out of the body tag and parse it into JSON object... 但是我没有从body标签中获取JSON字符串并将其解析为JSON对象...

Could anyone please help me with this problem? 谁有人可以帮我解决这个问题? Thank you so much!! 非常感谢!!

Here is my php code: 这是我的PHP代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery Autocompelete</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
</head>
<body>
<?php
    if(isset($_GET['symbol'])){
        $lookupURL = "http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=" . $_GET['symbol'];
        $jsonhtml = file_get_contents($lookupURL);
        echo $jsonhtml;
    }
?>
</body>
</html>

You need to parse the result twice: once as HTML using $.parseHTML() , then you can get the text from that and pass this into $.parseJSON() . 您需要将结果解析两次:一次使用$.parseHTML()作为HTML,然后您可以从中获取文本并将其传递给$.parseJSON()

Something along these lines: 这些方面的东西:

success: function(data){  //html string in this data parameter
    var html = $.parseHTML(data);
    var body = $(html).text();
    var json = $.parseJSON(body);
    // use the JSON data here
}

The Markit On Demand API supports JSON, so there's something wrong with your original query. Markit On Demand API支持JSON,因此您的原始查询有问题。

Have a look at this URL for an example: 请查看此URL以获取示例:
http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=AAPL http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=AAPL

It returns pure JSON data, which can be processed using $.parseJSON(data) 它返回纯JSON数据,可以使用$.parseJSON(data)

UPDATE: Try this code for example: 更新:尝试此代码,例如:

var requestData = 'symbol=AAPL';
$.ajax({
    url: "http://dev.markitondemand.com/MODApis/Api/v2/Quote/json",
    method: "get",
    data: requestdata,
    success: function(data){  //html string in this data parameter
        $symbolResponse = $.parseJSON(data);
    }
});

UPDATE 2: Use this PHP Code: (Just this code and nothing else) 更新2:使用此PHP代码:(只是这个代码,没有别的)

<?php

if (isset($_GET['symbol'])) {
    header('Content-Type: application/json');
    $lookupURL = "http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=" . $_GET['symbol'];
    $jsonhtml = file_get_contents($lookupURL);
    echo $jsonhtml;
}

?>

您可以选择JSON.parse(data)$.parseJSON(data)或未建议的eval(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM