如何在php中对JSON文件执行GET请求


How to perform GET requests on a JSON file in php?

我有一个json文件,格式为:

[{"label":"apple","desc":"fruit"},
{"label":"banana","desc":"fruit"},
{"label":"celery","desc":"vegetable"},
{"label":"plum","desc":"fruit"},
{"label":"","desc":"fruit"}]

如何对这些数据执行GET请求?例如

http://www.mysite.com/data?desc=vegetable将向浏览器和显示[{"label":"celery","desc":"vegetable"}]

http://www.mysite.com/data?label=apple&desc=fruit将显示[{"label":"apple","desc":"fruit"}]到浏览器

下面是一个我想模仿的工作示例:

http://ws.geonames.org/searchJSON(不返回任何内容)http://ws.geonames.org/searchJSON?featureClass=P&style=full&maxRows=12&name_startsWith=paris(返回匹配的城市。)

Jquery和php是我正在使用的工具。我正在尝试设置它,以便可以将jQueryUI自动完成与远程JSONP数据源一起使用。http://jqueryui.com/demos/autocomplete/#remote-jsonp

将其存储在数据库(例如MySQL)中可能会获得更好的结果。然后,您可以根据labeldesc列进行查询。然后,使用fetchObject访问结果行,并使用json_encode将对象直接转换为JSON。

根据传入的GET参数,您可能需要几个不同版本的查询。

    $.ajax({
        type: 'GET',
        url: '/getFood/',
         dataType: 'json',
        success: function(data) {
             // do whatever you want with the data here (like show in a div)
        },
        // replace with whatever you wanna send in the querystring
        data: {'desc':'vegetable',},
    });

在服务器端,检查GET请求中的"desc"参数并发送适当的响应。