如何从三个不同的json对象创建单个json对象

时间:2017-12-29 05:07:53

标签: javascript angularjs json

我想从三个不同的JSON对象创建单个JSON对象,例如。

包含JSON值的三个变量:

1。 GTX_INNER_JOIN 2. GTX_LEFT_OUTER_JOIN 3. GTX_RIGHT_OUTER_JOIN

所有这些都具有相同的(键,值)对。我想要单个JSON包含来自所有这些的值。

我尝试过angularJS并尝试 Angular:复制,扩展和合并,但我没有得到我想要的结果。

<?php
    $this->db->select("a.lead_id,a.business_name,a.a.contact_name_first,a.contact_name_last,a.work_email,a.contact_email,a.work_number,a.cell_no,a.city,a.state,a.zip,a.date_added,IF( a.label_type <> 0,'Important',' ') as label, b.company_name");
    $this->db->from('leads a');
    $this->db->join('companies b', 'b.id = a.company');
    $this->db->join('lead_note c', 'c.lead_id = a.id');

    $query = $this->db->get();
    $result = $query->result_array();

    if($query->num_rows() > 0){
        $delimiter = ",";
        $filename = "members_" . date('Y-m-d') . ".csv";

        //create a file pointer
        $f = fopen('php://memory', 'w');

        //set column headers
        $fields = array('Lead ID','Business','First Name','Last Name','Email','Personal Email','Work Number','Cell No','City','State','Zip','Date Added');

        //output each row of the data, format line as csv and write to file pointer
        foreach($result as $row){

            $lineData = array($row['lead_id'], $row['contact_name_first'], $row['contact_name_last'], $row['work_email'], $row['contact_email'], $row['work_number'], $row['cell_no'], $row['city'], $row['state'], $row['zip'], $row['date_added'], $row['label'], $row['company_name']);
            fputcsv($f, $lineData, $delimiter);
        }

        //move back to beginning of file
        fseek($f, 0);

        //set headers to download file rather than displayed
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '";');

        //output all remaining data on a file pointer
        fpassthru($f);
    }
    exit;

    ?>

我想要单个JSON,例如

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{ },{},{},{},{},{},{},{},...]

像这样,但这是创建JSON数组,如:

对象:

[GTX_INNER_JOIN:[{},{},{},{},{},....],

GTX_LEFT_OUTER_JOIN:[{},{},{},{},{} ,. ...],

GTX_RIGHT_OUTER _JOIN:[{},{},{},{},{},....]]

2 个答案:

答案 0 :(得分:0)

指定您期望的结果会非常有帮助。你可以做 -

var vGreenTXJSON = {
    innerJoin: vGreenTxData.GTX_INNER_JOIN,
    leftOuterJoin: vGreenTxData.GTX_LEFT_OUTER_JOIN,
    rightOuterJoin: vGreenTxData.GTX_RIGHT_OUTER_JOIN
};

答案 1 :(得分:0)

如果您直接将vGreenTxData的所有属性复制到vGreenTXJSON,则可以在下方使用。

angular.extend(vGreenTXJSON ,vGreenTxData);

如果您想从vGreenTxData对象中专门抓取一些道具,那么它将如下所示。

angular.extend(vGreenTXJSON ,{
    GTX_InnerJoin: vGreenTxData.GTX_INNER_JOIN,
    GTX_OuterJoin: vGreenTxData.GTX_LEFT_OUTER_JOIN,
    GTX_Right_OuterJoin: vGreenTxData.GTX_RIGHT_OUTER_JOIN
});