【问题标题】:Parsing JSON data and it's objects into different PHP functions将 JSON 数据及其对象解析为不同的 PHP 函数
【发布时间】:2015-11-01 17:21:29
【问题描述】:

我一直在环顾四周,希望能找到我遇到的问题……但是我找不到。我在 PHP 中创建了一个类,在该类中有一个连接到检索 JSON 数据的 API 的函数(这个函数已经过测试,并且工作起来就像一个魅力)。但是现在我正在尝试将收到的 JSON 数据的对象分离到不同的函数中。

我现在只能通过 foreach 和 echo 解析数据,但经常这样做会很麻烦。

这是它目前的样子,所以你有一个想法。

编辑

问题已解决,public function foo(); Had 和 $info 变量有一个 json_decode();,它中断了下一个拆分数据的函数。

class parseData{

    var $name;
    var $domain;

    public function foo($name, $domain)

    {

        $this->name = $name;
        $this->domain = $domain;

        $ch = curl_init();

        $user_agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';

        $request_headers = array();
        $request_headers[] = 'User-Agent: ' . $user_agent;
        $request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8';

        curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users?name=" . $name . "");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users?name=" . $name);

        $id = json_decode(curl_exec($ch));

        if (isset($id) && $id->profileVisible == 1) {

            curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users/" . $id->uniqueId . "/profile");
            $info = curl_exec($ch);

        } else
            $info = false;

        curl_close($ch);

        return $info;

    }

    public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();
    public function __construct(){
        $this->response = $this->foo("user", "nl");
        $this->init();
    }

    // this function will split data into sub variables
    private function init(){
        $this->response =json_decode(file_get_contents($this->response), TRUE);
        $this->user = $this->response['user'];
        $this->friends = $this->response['friends'];
        $this->groups = $this->response['groups'];
        $this->rooms = $this->response['rooms'];
        $this->badges = $this->response['badges'];
        // free main response object
        unset($this->response);
    }

    public function uid(){
        echo $this->user['uniqueId'];
    }

    public function name(){
        echo $this->user['name'];
    }

    public function membersince(){
        echo $this->user['memberSince'];
    }

    public function motto(){
        echo $this->user['motto'];
    }

    public function figure(){
        echo $this->user['figureString'];
    }

    //this function will manipulate USER data.
    public function user(){
        echo "Naam: ".$this->user['name'].'<br/>';
        echo "Motto: ".$this->user['motto'].'<br/>';
        echo "Lid sinds: ".$this->user['memberSince'].'<br/>';
        echo "Unique ID: ".$this->user['uniqueId'].'<br/>';
        echo "figureString: ".$this->user['figureString'].'<br/>';
        foreach($this->user['selectedBadges'] as $selectedBadge){
            echo 'Badge index: '. $selectedBadge['badgeIndex'];
            echo 'Badge code: '. $selectedBadge['code'];
            echo 'Badge naam: '. $selectedBadge['name'];
            echo 'Badge beschrijving: '. $selectedBadge['description'];
        }
    }
    public function friends(){
        //do stuff with $this->friends.
    }
    public function groups(){
        //do stuff with $this->groups
    }
    //and other functions like badges and rooms etc.
}

$parser = new parseData();
$parser->user();

API 会发回不同的对象,我想将它们分成不同的functions,如“用户”、“朋友”、“组”等,以便从这些对象中检索所有字符串。所以我的问题是,是否可以通过同一类中的不同函数解析 API 数据?如果是这样,我该怎么做?调用该函数是否也会变得更容易,还是会像使用foreach 方法一样麻烦?

【问题讨论】:

  • 请在你的输出中包含 sn-p
  • JSON 数据的输出?
  • 是的,JSON 数据的输出
  • 为了保持匿名,我编辑了一些结果,但结构是相同的。 {"user":{"uniqueId":"aaaa-662628f1fb34cbf4346103dff1685508","name":"foo","slogan":"Safety has no time-out!","memberSince":"2004-07-08T04:56:03.000+0000","profileVisible":true,"lastWebAccess":null}
  • 您想在主键{"user": 的基础上分离响应,对吗?因为可能有“朋友”、“群组”等,你想从特定的函数中调用,比如如果用户,调用 user(),如果是朋友,调用friends(),对吧?

标签: php json function class parsing


【解决方案1】:

所以,我查看了你的 json,你可以喜欢这个

class parseData{
  public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();
  public function __construct(){
    $this->response = call_your_function_get_api_response();
    $this->init();
  }
  // this function will split data into sub variables
  private function init(){
    $this->response = json_decode(file_get_contents('json.json'), TRUE);
    //I'm using file get contents here, as I've stored your json into file.
    // but you'll have to do like this
    //$this->response = json_decode($this->response, TRUE);
    $this->user = $this->response['user'];
    $this->friends = $this->response['friends'];
    $this->groups = $this->response['groups'];
    $this->rooms = $this->response['rooms'];
    $this->badges = $this->response['badges'];
    // free main response object
    unset($this->response);
  }
  /*
    * I'm updating here with new function name(), so to print name only do this.
  */
  public function name(){
    echo "Name: ".$this->user['name'].'<br/>'; //This will print name only.
  }
  //this function will manipulate USER data.
  public function user(){
    echo "User's Unique ID: ".$this->user['uniqueId'].'<br/>';
    echo "Name: ".$this->user['name'].'<br/>';
    echo "figureString: ".$this->user['figureString'].'<br/>';
    foreach($this->user['selectedBadges'] as $selectedBadge){
        echo 'Badge index: '. $selectedBadge['badgeIndex'];
        echo 'Badge code: '. $selectedBadge['code'];
        echo 'Badge name: '. $selectedBadge['name'];
        echo 'Badge description: '. $selectedBadge['description'];
    }
  }
  public function friends(){
    //do stuff with $this->friends.
  }
  public function groups(){
    //do stuff with $this->groups
  }
  //and other functions like badges and rooms etc.
}
$parser = new parseData();
$parser->user();

/*
 * and you can call function like this
 * $parser->friends();      will process only friends data from json response
 * $parser->groups();       will process only groups data from json response.
 * ... rest of functions ...
*/

我已经使用users() function 进行了一次迭代测试。

这里是输出

编辑

我已经用新方法 name() 更新了 class,所以现在只打印名称,

$parser->name();

另一个编辑

根据您的评论

我再次将__construct() 代码放在这里。

public function __construct(){
  $get = new foo();
  $this->response = $get->bar("person", "nl");
  $this->init();
}

其余的都是一样的。

$parser = new parseData();
$parser->user();
$parser->name();  //will display name only.

将所有内容放在一起

你需要更新两个functions

public function __construct(){
    $this->response = json_decode($this->foo("user", "nl"), TRUE);
    $this->init();
}

init() 中注释掉第一行,因为它不再需要了。

希望这会对你有所帮助。

【讨论】:

  • 谢谢!虽然我有一个问题,但如果我只需要回显“名称”怎么办?这也可能吗?如果我是正确的,这是一个单独的班级还是他们必须在同一个班级?
  • 我其实没看懂最后一部分,但是,所有这些函数都在同一个类中。并且只打印名称,我正在更新我的答案:)
  • 基本上,我在“foo”类中有响应函数,但我想问的是该函数是否需要在 ParseData 函数中。然而,我已经对自己回答了这个问题,这是一个愚蠢的问题。感谢推人,我很感激。
  • 这取决于你,无论你想把这个功能放在哪里。如果它在foo 类中,那么在这个类中,你可以这样做,在__construct() 方法中,添加这一行。 $get = new foo(); $this-&gt;response = $get-&gt;response();
  • 好的,现在它似乎可以工作了,我已经用只有 API 响应所在的类扩展了你的类。但是,我现在该如何测试呢?因为 API 类中的函数仍然依赖于两个变量。 ($名称,$域)。我这样做吗:$get = new foo(); $getdata = $get-&gt;bar("person", "nl"); $parser = new parseData($getdata); $parser-&gt;user();
猜你喜欢
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多