解析JSON游离碱导致PHP [英] Parse JSON Freebase results in PHP

查看:148
本文介绍了解析JSON游离碱导致PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很抱歉,如果这是太基本的,但我真的不知道如何做到这一点。

I'm really sorry if this is too basic, but I really don't know how to do this.

我使用这个jQuery自动完成插件:的http:// devthought。 COM /可湿性粉剂内容/项目/ jQuery的/ textboxlist /演示/

I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

编辑:这是jQuery的code I用于自动完成:

This is the jquery code i use for the autocomplete:

$(function() {
        var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
                minLength: 2,
                queryRemote: true,
                remote: {url: 'autocomplete2.php'}
            }}});

插件使用了自动完成PHP的,这就是一个例子,它返回的输出:标识,文字,空(HTML我不需要),一些HTML

$response = array();
            $names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'Etc');

            // make sure they're sorted alphabetically, for binary search tests
            sort($names);

            $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

            foreach ($names as $i => $name)
            {
                if (!preg_match("/^$search/i", $name)) continue;
                $filename = str_replace(' ', '', strtolower($name));
                $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
            }

            header('Content-type: application/json');
            echo json_encode($response);

我需要一个类似的PHP来处理这个结果:<一href=\"http://www.freebase.com/private/suggest?$p$pfix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=\" rel=\"nofollow\">http://www.freebase.com/private/suggest?$p$pfix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

...是的甲壳虫的$搜索值,并获得此输出:

...being "beatles" the $search value, and getting this output:

guid,"name",null,"name<span>n:type name</span>"

那么,第一个结果将是:

So, the first result would be:

0,"The Beatles",null,"The Beatles<span>Band</span>"

当然,我需要从PHP查询freebase.com。我的意思是:

Of course I would need to query freebase.com from that PHP. I mean:

        +---------------+         +-----------+        +------------+
        |               |         |           |        |            |
        |  TextboxList  +-------->|   PHP     +------->|  Freebase  |
        |               |         |           |        |            |
        +---------------+         +-----------+        +------+-----+
                                                              |
             JSON                     JSON                    |
          TextboxList   <--------+  freebase       <----------+

这可能吗?谢谢!

Is this possible? Thanks!

推荐答案

试试这个:

$response = array();

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

$myJSON = file_get_contents('http://www.freebase.com/private/suggest?prefix=' . urlencode($search));

$musicObj = json_decode($myJSON); // Need to get $myJSON from somewhere like file_get_contents()

foreach ($musicObj->result as $item)
{
    $response[] = array($item->guid, $item->name, null, $item->name . '<span>'.$item->{'n:type'}->name.'</span>');
}

header('Content-type: application/json');
echo json_encode($response);

第一JSON转义的结果,然后给出了:

The first JSON-escaped result then gives:

["#9202a8c04000641f800000000003ac10","The Beatles",null,"The Beatles<span>Band<\/span>"]

但是,尽管这一切,你真的不需要使用PHP都做到这一点。您可以将所有从JavaScript做到这一点,避免额外的行程到服务器。如果提供了回调参数中游离碱,它可以创建JSONP(这是JSON包裹在一个函数的调用,使用你选择的函数名),你可以得到jQuery中,然后在JavaScript中进一步操纵,以自己的喜好。但上面的每原来的做法,使用PHP。

But despite all this, you really don't need to use PHP at all to do this. You can do this all from JavaScript and avoid an extra trip to your server. If you supply the callback argument to freebase, it can create JSONP (which is JSON wrapped in a call to a function, using a function name of your choice) which you can obtain in jQuery and then manipulate further in JavaScript to your liking. But the above is per your original approach in using PHP.

这篇关于解析JSON游离碱导致PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆