我有一些jQuery代码使用getJSON()检索内容.有n个 JSON文件,可根据需要从服务器中检索:
/json-content/data0.json
/json-content/data1.json
/json-content/data2.json
等等...
相反,我想将所有JSON存储在单个文件中,以减少检索数据所需的HTTP请求数.
将JSON文件合并为一个的最佳方法是什么?如果我将JSON文件连接在一起,它就不再适用于getJSON().
更新 - 澄清了我的问题
我试图使用grep过滤json对象数组,以便搜索数组,如果任何键#2-6的值为yes,则返回键1和7的值.
数组在下面 - 换句话说,如果'location'键的任何值为yes,则名称和描述将作为列表项返回.
很感谢任何形式的帮助.
[
{
"name": "name",
"location1": "no",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
},
{
"name": "name",
"location1": "yes",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
}
]
Run Code Online (Sandbox Code Playgroud) 当通过json调用获取项目列表时,最好使用几个小调用(根据需要获取信息)或一个包含所有数据的大调用.
例如,您有一个json调用来获取与特定标题关键字匹配的书籍列表.有100个结果.您将以分页形式显示数据 - 每个"页面"10个结果.拨打一个电话并获得所有结果或在每个页面上拨打下一个10的电话会更有效吗?
我认为这部分取决于有多少结果.如果它是一个巨大的数字,第二个选项似乎很清楚.但是,对于一次通话中可以获得的数量有多少限制--100,1000,10000项?
这可能是非常基本的,但我正在尝试测试Google Places API.我正在浏览文档并使用它们提供的一些示例.我正在尝试使用JQuery getJSON函数,因为我已经能够成功地使用它来异步访问外部JSON文件,因此我认为这可能是获取Google Places查询的JSON结果的好方法.这是我正在尝试使用的代码:
<body>
<div id="message"></div>
<script type="text/javascript">
var requestURL = 'https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key='my_google_places_key';
$(document).ready(function () {
$.getJSON(requestURL, function (data) {
for (i = 0; i < data.results.length; i++) {
myAddress[i] = data.results[i].formatted_address;
document.getElementById("message").innerHTML += myAddress[i] + "<br>";
console.log(myAddress[i]);
}
});
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
根据文档,对查询的结果JSON响应应如下所示:
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"formatted_address" : "529 Kent Street, Sydney NSW, Australia",
"geometry" : {
"location" : {
"lat" : -33.8750460,
"lng" : 151.2052720
} …Run Code Online (Sandbox Code Playgroud) 我的问题是:
当我使用以下代码调用json时
var url="http://localhost:9000/json";
$.getJSON(url,
function(data){
alert(data['yay']);
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是,我的localhost IP是10.0.0.145,当我用IP地址替换localhost时,它不再有效
var url="http://10.0.0.145:9000/json";
$.getJSON(url,
function(data){
alert(data['yay']);
});
Run Code Online (Sandbox Code Playgroud) 我正在使用CoffeeScript来执行getJSON请求:
$.getJSON(
"http://maps.googleapis.com/maps/api/distancematrix/json?callback=?"
origins: origin
destinations: destinations
sensor: false
success: (data) ->
console.log data
error: (data) ->
console.log data
'json'
)
Run Code Online (Sandbox Code Playgroud)
网址是:
http://maps.googleapis.com/maps/api/distancematrix/json?callback=?&origins=-25.8350643,28.1636066&destinations=-25.551836,%2028.423075|-25.218503,%2027.923075|&sensor=false
Run Code Online (Sandbox Code Playgroud)
如果你把它放在你的浏览器中,它将返回JSON,但是ajax请求告诉我:
Uncaught SyntaxError: Unexpected token:
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我是javascript,angularJS和JQuery的新手,但我刚刚开始编写一个angularJS应用程序,我使用JQuery从这样的网络服务器获取JSON:
var obj = $.getJSON( "http://something.com/lol?query="+ $scope.searchString, function() {
$scope.items = obj.responseJSON.entries;
}
Run Code Online (Sandbox Code Playgroud)
在angularJS中有一个等于$ .getJSON的方法吗?这样我就不必导入JQuery库了.
提前谢谢,新手.
到目前为止这是我的解决方案:
function InstantSearchController($scope, $http){
$scope.search = function() {
$http.jsonp("http://something.com/lol?query="+ $scope.searchString + "?json_callback=JSON_CALLBACK").success(
function(data, status) {
console.log(data);
}
);
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误信息:
未捕获的SyntaxError:意外的令牌:
为什么是这样?我究竟做错了什么?}
目标
用户登录并在成功授权后,在登录表单所在的同一位置(从MongoDB数据库)加载管理页面:
登录表单>提交[成功]>从表格所在的同一位置的数据库加载的内容
我试过的
我想我知道解决方案中涉及的大部分'位',但是还没能将它们全部放在一起,例如:
template1.tpl
这是一个view包含jQuery 的瓶子,它用于getJSON()与包含route查询MongoDB数据库的瓶子的Python文件(基于点击的元素'href'值)进行通信,因此返回动态内容:
<script>
function loadContent(href){
$.getJSON("/my_route", {cid: href, format: 'json'}, function(results){
$("#content_area").html("");
$("#content_area").append(results.content);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
my_application.py
@route('/my_route')
def my_function():
# set up connection
dbname = 'my_db_name'
connection = pymongo.MongoClient(os.environ['OPENSHIFT_MONGODB_DB_URL'])
db = connection[dbname]
# get the data sent from the getJSON() request
href = request.GET.cid
# specify collection based on href value
if href =="x" or href=="y" or href=="z":
collection = db.collection1
elif href=="j":
collection = db.collection2
else: …Run Code Online (Sandbox Code Playgroud) 我正在使用发送HTTP请求的Sharefile API并获得各自的答案.它们是通过URL制作的,并始终使用相同的功能.这里是.
Private Function InvokeShareFileOperation(ByVal requestUrl As String) As JObject
Dim request As HttpWebRequest = WebRequest.Create(requestUrl)
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim json As String = reader.ReadToEnd()
response.Close()
Return JObject.Parse(json)
End Function
Run Code Online (Sandbox Code Playgroud)
由于某些操作有点长,我需要以某种方式跟踪他们正在进行的进度并且不知道如何进行.后来我打算使用这个进度并从中创建一个进度条.
(编辑)顺便说一句,这是占用大部分时间的第二个代码行(下面),即跟踪操作.
Dim response As HttpWebResponse = request.GetResponse()
Run Code Online (Sandbox Code Playgroud) 没有jQuery,这种方法相当于什么?
$(function() {
$.getJSON("datas.json", function(e) {
var t = [];
$.each(e, function(e, t) {
$("div#" + e).text(t)
})
})
})
Run Code Online (Sandbox Code Playgroud)
谢谢.
getjson ×10
jquery ×6
json ×5
javascript ×3
ajax ×2
angularjs ×1
bottle ×1
coffeescript ×1
httprequest ×1
httpresponse ×1
mongodb ×1
performance ×1
python-2.7 ×1
syntax-error ×1
vb.net ×1