将JSON数据及其对象解析为不同的PHP函数

时间:2015-11-01 17:21:30

标签: php json function class parsing

我一直在四处寻找希望能找到我磕磕绊绊的问题...但是我找不到它。我在PHP中创建了一个类,在该类中有一个连接到API检索JSON数据的函数(此函数已经过测试并且像魅力一样工作)。但是现在我试图将我收到的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方法一样麻烦?

1 个答案:

答案 0 :(得分: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进行了一次迭代测试。

这是输出

enter image description here

修改

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

$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()注释掉第一行,因为它不再需要了。

希望这会对你有所帮助。