我从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>
上面的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
}
});
});
});
但是我没有从body标签中获取JSON字符串并将其解析为JSON对象......
有人可以帮我解决这个问题吗?非常感谢你!!
这是我的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>
答案 0 :(得分:2)
您需要将结果解析两次:一次使用dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.parse:parseui-login-android:0.0.1'
compile 'com.parse:parseui-widget-android:0.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.parse:parse-android:1.13.0'
compile 'com.commit451:PhotoView:1.2.5'
作为HTML,然后您可以从中获取文本并将其传递到$.parseHTML()
。
这些方面的东西:
$.parseJSON()
答案 1 :(得分:2)
Markit On Demand API支持JSON,因此原始查询出现问题。
请查看此网址以获取示例:
http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=AAPL
它返回纯JSON数据,可以使用$.parseJSON(data)
更新:请尝试以下代码:
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);
}
});
更新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;
}
?>
答案 2 :(得分:-1)
您可以选择$.parseJSON(data)
,eval(data)
或不明智的<item>
。