簡體   English   中英

如何在json文件中添加或插入記錄

[英]how to add or insert record in json file

我如何使用php或javascript在json文件中添加或插入記錄(以數組形式)?

我已經嘗試過了,但是沒有運氣:

$file = file_get_contents('Copy.json');
            $tempArray = json_decode($file);

            $arr = array('type' => 'Feature',
                        'id'=>$adsID,
                        'properties' => array(
                        'IMAGE'=>$ALogo,
                        'NAME'=>$name,
                        'TEL'=>$ATel,
                        'URL'=>$url,
                        'ADRESS1'=>$A_add,
                        'ADDRESS2'=>'add2',
                        'CITY'=>'Angeles City',
                        'ZIP'=>'2009'),
                            'geometry' => array(
                            'type' => 'Point',
                            'coordinates' => array ($ACoords1))
                        ) ;// features


            echo json_encode($arr);//$data[] = $_POST['data'];

            array_push($tempArray, $arr);
            $jsonData = json_encode($tempArray);
            file_put_contents('Copy.json', $jsonData);

數組合並$ file = file_get_contents('Copy.json'); $ tempArray = json_decode($ file);

        $arr = array('type' => 'Feature',
                    'id'=>$adsID,
                    'properties' => array(
                    'IMAGE'=>$ALogo,
                    'NAME'=>$name,
                    'TEL'=>$ATel,
                    'URL'=>$url,
                    'ADRESS1'=>$A_add,
                    'ADDRESS2'=>'add2',
                    'CITY'=>'Angeles City',
                    'ZIP'=>'2009'),
                        'geometry' => array(
                        'type' => 'Point',
                        'coordinates' => array ($ACoords1))
                    ) ;// features


        echo json_encode($arr);//$data[] = $_POST['data'];

        $merge = array_merge($tempArray, $arr);
        $jsonData = json_encode($merge);
        file_put_contents('Copy.json', $jsonData);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM