将数据集生成为Json而不是一个大的混合Json

时间:2015-07-10 17:35:22

标签: php json angularjs

如何创建一个Json,其中包含以逗号分隔的许多小数据集?

而不是用大括号括起来的一个大Json?

我确实收到了一个Json和php,我确实使用foreach来循环它,在里面进行大量的数据处理。

然后生成一个新的Json,只是为了避免客户端的数据处理将由angularjs ng-repeat处理。

所有json数据都混合成一个大的json集(在双花括号内)

我的目标是分成几组小数据。

我可以使用NrType属性。在此脚本中,NrType接收最后一次归因,并且只有最后一次接收可用。

//The php script

    $arr = json_decode($returnedJson); //The original json to be pre-processed
    $processedData = "[]";
    $processedJson = json_decode($processedData,true);

    foreach($arr as $key=>$value) { 
        foreach($value as $vkey=>$vvalue) {             

            if( $value[$i]->NrType == 1 ) {

                $VlMIni     = $value[$i]->QttyInitial;
                $VlMSub     = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
                $VlMRec     = $value[$i]->RealValue;
                $VlMTotal   += $VlMesRece;
                //much more data processing going on here ...

            } elseif( $value[$i]->NrType == 2 ) { 

            .
            .
            .
            //and much more data processing going on here ...
        }
    }

    //simple data atribution here
    $processedJson['labelDesIni']       = 'Instruments';
    $processedJson['labelValueMIni']    = $lblVlMIni;   
    $processedJson['labelValuePIni']    = $lblVlPlIni; 
    $processedJson['labelValueAIni']    = $lblVlAIni;   
    $processedJson['labelValuePAIni']   = $lblVlPAIni;  
    $processedJson['labelValuePercInic']    = $lblVlPercInic;   
    $processedJson['labelValuePerc2Inic']   = $lblVlPerc2Inic; 

    //much more data atribution ...

    echo json_encode($processedJson); //the new hgenerated Json

生成的Json:

{
    labelDesI: "Inspection",
    labelValueMI: "2357",
    labelValuePlI: "3914066",
    labelValueAI: "1389406",
    labelValuePAI: "2431425",
    labelValuePercI: 57.143691456656,
    labelValuePerc2I: 35.497766261478,
    labelDesR: "Instruments",
    labelValueMR: "734.54",
    labelValuePR: "819.14",
    labelValueAR: "660.05",
    labelValuePAR: "877.94",
    labelValuePercR: 80.087,
    labelValuePerc2R: 44.739,
    labelDesAcfi: "Fiscalização",
    labelValueMAcfi: "343",
    labelValuePlAcfi: "29907",
    labelValueAAcfi: "16718",
    labelValuePAAcfi: "16493",
    labelValuePercAcfi: 101.36421512157,
    labelValuePerc2Acfi: 55.899956531916,
    labelDesT: "Totals",
    labelValueMT: 365.59,
    labelValuePlT: 547.62,
    labelValueAnT: 909.63,
    labelValuePAnT: 957.63,
    labelValuePercT: 22949,
    labelValuePerc2T: 25065
}

所需的格式是:

{
    label: "Inspection",
    labelValue1: "2357",
    labelValue2: "3914066",
    labelValue3: "1389406",
    labelValue4: "2431425",
    labelValue5: 57.1456656,
    labelValue6: 35.4961478
},
{
    labelDesR: "Instruments",
    labelValue1: "734.54",
    labelValue2: "819.14",
    labelValue3: "660.05",
    labelValue4: "877.94",
    labelValue5: 80.087,
    labelValue6: 44.739
},
{
    labelDesT: "Totals",
    labelValue1: 365.59,
    labelValue2: 547.62,
    labelValue3: 909.63,
    labelValue4: 957.63,
    labelValue5: 22949,
    labelValue6: 25065
}

提前感谢

1 个答案:

答案 0 :(得分:1)

分别生成所有对象,创建这些对象的数组并对数组进行json_encode:

$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];

并将其添加到主对象:

$processedJson[] = $processedJsonElement;

为您要表示的Json的每个部分执行此操作。不确定你是如何构造Foreach循环的,因为你的代码与输出不匹配,但无论你构造什么,你调用json_encode时都会输出。

基本上,您需要构建foreach循环,以便能够将要表示的对象划分为json对象数组。